diff --git a/package-lock.json b/package-lock.json index a8054a8..5d4743e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,6 +15,7 @@ }, "devDependencies": { "@biomejs/biome": "1.5.3", + "@types/node": "20.14.12", "esmock": "2.6.3", "nock": "13.5.1", "undici": "6.5.0" @@ -217,9 +218,9 @@ "integrity": "sha512-nG96G3Wp6acyAgJqGasjODb+acrI7KltPiRxzHPXnP3NgI28bpQDRv53olbqGXbfcgF5aiiHmO3xpwEpS5Ld9g==" }, "node_modules/@types/node": { - "version": "20.11.0", - "resolved": "https://registry.npmjs.org/@types/node/-/node-20.11.0.tgz", - "integrity": "sha512-o9bjXmDNcF7GbM4CNQpmi+TutCgap/K3w1JyKgxAjqx41zp9qlIAVFi0IhCNsJcXolEqLWhbFbEeL0PvYm4pcQ==", + "version": "20.14.12", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.14.12.tgz", + "integrity": "sha512-r7wNXakLeSsGT0H1AU863vS2wa5wBOK4bWMjZz2wj+8nBx+m5PeIn0k8AloSLpRuiwdRQZwarZqHE4FNArPuJQ==", "dependencies": { "undici-types": "~5.26.4" } @@ -1980,9 +1981,9 @@ "integrity": "sha512-nG96G3Wp6acyAgJqGasjODb+acrI7KltPiRxzHPXnP3NgI28bpQDRv53olbqGXbfcgF5aiiHmO3xpwEpS5Ld9g==" }, "@types/node": { - "version": "20.11.0", - "resolved": "https://registry.npmjs.org/@types/node/-/node-20.11.0.tgz", - "integrity": "sha512-o9bjXmDNcF7GbM4CNQpmi+TutCgap/K3w1JyKgxAjqx41zp9qlIAVFi0IhCNsJcXolEqLWhbFbEeL0PvYm4pcQ==", + "version": "20.14.12", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.14.12.tgz", + "integrity": "sha512-r7wNXakLeSsGT0H1AU863vS2wa5wBOK4bWMjZz2wj+8nBx+m5PeIn0k8AloSLpRuiwdRQZwarZqHE4FNArPuJQ==", "requires": { "undici-types": "~5.26.4" } diff --git a/package.json b/package.json index d2b997d..cdb6ccd 100644 --- a/package.json +++ b/package.json @@ -3,14 +3,14 @@ "private": true, "type": "module", "scripts": { - "test": "node --test ./src/*.test.js", - "dev": "node --test --watch ./src/*.test.js", + "test": "node --test --experimental-test-module-mocks ./src/*.test.js", + "dev": "node --test --watch --experimental-test-module-mocks ./src/*.test.js", "test:all": "npm run test:coverage && npm run test:only && npm run test:seq && npm run test:par", - "test:coverage": "node --test --experimental-test-coverage ./src/*.test.js", - "test:coverage:file": "node --test --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=lcov.info ./src/*.test.js", + "test:coverage": "node --test --experimental-test-coverage --experimental-test-module-mocks ./src/*.test.js", + "test:coverage:file": "node --test --experimental-test-coverage --experimental-test-module-mocks --test-reporter=lcov --test-reporter-destination=lcov.info ./src/*.test.js", "report:html": "npx @lcov-viewer/cli lcov -o ./coverage ./lcov.info", - "test:seq": "node --test --test-concurrency=1 ./src/*.test.js", - "test:par": "node --test --test-concurrency=5 ./src/*.test.js", + "test:seq": "node --test --test-concurrency=1 --experimental-test-module-mocks ./src/*.test.js", + "test:par": "node --test --test-concurrency=5 --experimental-test-module-mocks ./src/*.test.js", "test:only": "node --test --test-only src/*.only.js", "test:failing": "node --test src/*.failing.js && echo 'Error: test run should fail' && exit 1 || echo 'Success: test run failed as expected' && exit 0", "lint": "biome lint --apply ./src", @@ -25,6 +25,7 @@ "supertest": "7.0.0" }, "devDependencies": { + "@types/node": "20.14.12", "@biomejs/biome": "1.5.3", "esmock": "2.6.3", "nock": "13.5.1", diff --git a/src/02.04-model-mock-native.test.js b/src/02.04-model-mock-native.test.js new file mode 100644 index 0000000..64f5140 --- /dev/null +++ b/src/02.04-model-mock-native.test.js @@ -0,0 +1,39 @@ +import { describe, test, mock, before, after } from "node:test"; +import assert from "node:assert/strict"; + +// mock.module() was added in 22.3.0 +const supportsMockModule = (() => { + const [major, minor, _patch] = process.versions.node.split("."); + return parseInt(major, 10) >= 22 && parseInt(minor, 10) >= 3; +})(); + +let Model; +/** @type {ReturnType}*/ +let modelMock; +describe("Class extends Mock - native", { skip: !supportsMockModule }, () => { + before(async () => { + modelMock = mock.module("sequelize", { + namedExports: { + Model: class {}, + }, + }); + + const { default: model } = await import("./02.04-model.js"); + Model = model; + }); + after(() => { + modelMock.restore(); + }); + test("It should not throw when passed a model containing an empty list of meetings", async (t) => { + const model = new Model(); + model.meetings = []; + assert.doesNotThrow(model.isAvailable.bind(model, new Date(Date.now()))); + }); + + test("It should not throw when passed a model containing an empty list of meetings", () => { + const model = Object.assign(new Model(), { + meetings: [], + }); + assert.doesNotThrow(model.isAvailable.bind(model, new Date(Date.now()))); + }); +});