Add some examples for strict equality

This commit is contained in:
Steve Kinney
2024-09-29 16:54:37 -06:00
parent 8f4ba6690c
commit 240bfb6dde
2 changed files with 55 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
{
"name": "strictly-speaking",
"version": "1.0.0",
"type": "module",
"scripts": {
"start": "vitest --ui",
"test": "vitest"
},
"repository": {
"type": "git",
"url": "git+https://github.com/stevekinney/testing-javascript.git"
},
"author": "Steve Kinney <hello@stevekinney.net>",
"license": "MIT",
"bugs": {
"url": "https://github.com/stevekinney/testing-javascript/issues"
},
"homepage": "https://github.com/stevekinney/testing-javascript#readme",
"devDependencies": {
"@vitest/ui": "^2.1.1",
"vite": "^5.4.5",
"vitest": "^2.1.1"
}
}

View File

@@ -0,0 +1,31 @@
import { test, expect } from 'vitest';
class Person {
constructor(name) {
this.name = name;
}
}
test('objects with the same properties are equal', () => {
expect({ a: 1, b: 2 }).toEqual({ a: 1, b: 2 });
});
test('objects with different properties are not equal', () => {
expect({ a: 1, b: 2 }).not.toEqual({ a: 1, b: 3 });
});
test('objects with undefined properties are equal to objects without those properties', () => {
expect({ a: 1 }).toEqual({ a: 1, b: undefined });
});
test('objects with undefined properties are *not* strictly equal to objects without those properties', () => {
expect({ a: 1 }).not.toStrictEqual({ a: 1, b: undefined });
});
test('instances are equal to object literals with the same properties', () => {
expect(new Person('Alice')).toEqual({ name: 'Alice' });
});
test('instances are not strictly equal to object literals with the same properties', () => {
expect(new Person('Alice')).not.toStrictEqual({ name: 'Alice' });
});