Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add native mock module example #29

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
39 changes: 39 additions & 0 deletions src/02.04-model-mock-native.test.js
Original file line number Diff line number Diff line change
@@ -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<typeof mock['module']>}*/
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())));
});
});