From 240bfb6dde6fcd1f455483754139a8da99d1f898 Mon Sep 17 00:00:00 2001 From: Steve Kinney Date: Sun, 29 Sep 2024 16:54:37 -0600 Subject: [PATCH] Add some examples for strict equality --- examples/strictly-speaking/package.json | 24 ++++++++++++++ .../strictly-speaking.test.js | 31 +++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 examples/strictly-speaking/package.json create mode 100644 examples/strictly-speaking/strictly-speaking.test.js diff --git a/examples/strictly-speaking/package.json b/examples/strictly-speaking/package.json new file mode 100644 index 0000000..1ef6feb --- /dev/null +++ b/examples/strictly-speaking/package.json @@ -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 ", + "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" + } +} diff --git a/examples/strictly-speaking/strictly-speaking.test.js b/examples/strictly-speaking/strictly-speaking.test.js new file mode 100644 index 0000000..f9de230 --- /dev/null +++ b/examples/strictly-speaking/strictly-speaking.test.js @@ -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' }); +});