Skip to content

Commit

Permalink
F7323980055: Switch from jest to vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
DamienCassou committed Apr 25, 2024
1 parent 739e2db commit a7b3a23
Show file tree
Hide file tree
Showing 12 changed files with 785 additions and 1,792 deletions.
8 changes: 7 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,19 @@ module.exports = {
ignoreExports: [
// List of files exporting stuff which are not imported:
"src/widgetjs.js",
"./vitest.config.js",
// List of files not exporting anything:
"**/.eslintrc.cjs",
"./babel.config.cjs",
"src/router/optionalParameterSegment.js",
"src/router/staticSegment.js",
],
},
],
},
settings: {
"import/resolver": {
exports: {},
node: true,
},
},
};
11 changes: 0 additions & 11 deletions babel.config.cjs

This file was deleted.

29 changes: 0 additions & 29 deletions jest.config.mjs

This file was deleted.

16 changes: 7 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,17 @@
},
"scripts": {
"lint": "eslint --ext js,cjs,mjs --max-warnings 0 --format unix --report-unused-disable-directives .",
"test:run": "jest",
"test:serve": "majestic"
"test:run": "vitest",
"test:serve": "vitest --ui"
},
"devDependencies": {
"@babel/core": "^7.24.4",
"@babel/plugin-transform-modules-commonjs": "^7.24.1",
"@foretagsplatsen/eslint-plugin": "^7.0.1",
"@jest/globals": "^29.7.0",
"@vitest/coverage-v8": "^1.5.0",
"@vitest/ui": "^1.5.0",
"eslint": "^8.57.0",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"jest-extended": "^4.0.2",
"majestic": "^1.8.1",
"eslint-import-resolver-exports": "^1.0.0-beta.5",
"jsdom": "^20.0.3",
"vitest": "^1.5.0",
"yaem": "foretagsplatsen/yaem#1.0.0"
},
"peerDependencies": {
Expand Down
14 changes: 7 additions & 7 deletions src/test/htmlCanvasTest.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import htmlCanvas from "../htmlCanvas.js";
import jQuery from "jquery";
import { describe, it, expect } from "@jest/globals";
import { describe, it, expect } from "vitest";

function withCanvas(callback) {
jQuery("BODY").append('<div id="sandbox"></div>');
Expand Down Expand Up @@ -127,7 +127,7 @@ describe("htmlCanvas", () => {

let div = html.div({ [attributeName]: undefined });

expect(div.element.hasAttribute("data-test")).toBeFalse();
expect(div.element.hasAttribute("data-test")).toBe(false);
});
});

Expand All @@ -139,7 +139,7 @@ describe("htmlCanvas", () => {

let div = html.div({ [attributeName]: null });

expect(div.element.hasAttribute("data-test")).toBeFalse();
expect(div.element.hasAttribute("data-test")).toBe(false);
});
});

Expand All @@ -151,7 +151,7 @@ describe("htmlCanvas", () => {

let div = html.div({ [attributeName]: false });

expect(div.element.hasAttribute("data-test")).toBeFalse();
expect(div.element.hasAttribute("data-test")).toBe(false);
});
});

Expand All @@ -163,7 +163,7 @@ describe("htmlCanvas", () => {

let div = html.div({ [attributeName]: "" });

expect(div.element.hasAttribute("data-test")).toBeFalse();
expect(div.element.hasAttribute("data-test")).toBe(false);
});
});
});
Expand Down Expand Up @@ -193,7 +193,7 @@ describe("htmlCanvas", () => {
// and click triggers callback
linkEl.click();

expect(clicked).toBeTrue();
expect(clicked).toBe(true);
});
});

Expand All @@ -218,7 +218,7 @@ describe("htmlCanvas", () => {
// and click triggers callback
linkEl.click();

expect(clicked).toBeTrue();
expect(clicked).toBe(true);
});
});

Expand Down
17 changes: 5 additions & 12 deletions src/test/router/hashLocationTest.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import jQuery from "jquery";
import hashLocationModel from "../../router/hashLocation.js";
import {
afterEach,
beforeEach,
describe,
expect,
it,
jest,
} from "@jest/globals";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

// Helpers

Expand Down Expand Up @@ -42,15 +35,15 @@ describe("hashLocation", () => {

my = {};
hashLocation = hashLocationModel({}, my);
jest.useFakeTimers();
vi.useFakeTimers();
});

afterEach(() => {
if (hashLocation) {
hashLocation.stop();
}
window.location.hash = "";
jest.useRealTimers();
vi.useRealTimers();
});

it("hash defaults", () => {
Expand Down Expand Up @@ -117,7 +110,7 @@ describe("hashLocation", () => {

it("setUrl() triggers change", () => {
let anotherHashLocation = hashLocationModel();
let spy = jest.fn();
let spy = vi.fn();

// Arrange: listen for url changes
anotherHashLocation.changed.register(spy);
Expand Down Expand Up @@ -184,6 +177,6 @@ describe("hashLocation", () => {
},
);

jest.advanceTimersByTime(131);
vi.advanceTimersByTime(131);
}));
});
2 changes: 1 addition & 1 deletion src/test/router/routeTest.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import router from "../../router.js";
import { describe, it, expect } from "@jest/globals";
import { describe, it, expect } from "vitest";

function assertMatch(url, route, _message) {
expect(
Expand Down
29 changes: 11 additions & 18 deletions src/test/router/routerTest.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import router from "../../router/router.js";
import {
afterEach,
beforeEach,
describe,
expect,
it,
jest,
} from "@jest/globals";
import { vi, afterEach, beforeEach, describe, expect, it } from "vitest";

function delayedSteps() {
let steps = Array.prototype.slice.call(arguments);
Expand Down Expand Up @@ -34,14 +27,14 @@ describe("router", () => {

my = {};
aRouter = router({}, my);
jest.useFakeTimers();
vi.useFakeTimers();
});

afterEach(() => {
aRouter.stop();
aRouter.clear();
aRouter = null;
jest.useRealTimers();
vi.useRealTimers();
});

it("router defaults", () => {
Expand Down Expand Up @@ -119,7 +112,7 @@ describe("router", () => {
it("resolveUrl executes route callback on match", () => {
// Arrange: setup a route
let userRoute = aRouter.addRoute({ pattern: "/user/" });
let spy = jest.fn();
let spy = vi.fn();

userRoute.on("matched", spy);

Expand All @@ -132,7 +125,7 @@ describe("router", () => {
});

it("resolveUrl triggers resolveUrl event", () => {
let spy = jest.fn();
let spy = vi.fn();

// listen for "resolveUrl event" on router
aRouter.on("resolveUrl", spy);
Expand Down Expand Up @@ -185,7 +178,7 @@ describe("router", () => {
}));

it("resolveUrl executes action on match", () => {
let spy = jest.fn();
let spy = vi.fn();

// Arrange: setup a route
aRouter.addRoute({
Expand Down Expand Up @@ -328,7 +321,7 @@ describe("router", () => {
}));

it("add route with constraints", () => {
let action = jest.fn();
let action = vi.fn();

aRouter.addRoute({
pattern: "/user/#name/",
Expand Down Expand Up @@ -377,7 +370,7 @@ describe("router", () => {
it("pipe notfound to another router", () => {
// Arrange another router with a route handler
let anotherRouter = router();
let spy = jest.fn();
let spy = vi.fn();

anotherRouter.addRoute({
pattern: "APathNotInDefaultRouterButInPipedRouter",
Expand All @@ -397,7 +390,7 @@ describe("router", () => {
it("pipe route to another router", () => {
// Arrange another router with a route handler
let anotherRouter = router();
let spy = jest.fn();
let spy = vi.fn();

anotherRouter.addRoute({
pattern: "/a/b/#c",
Expand Down Expand Up @@ -459,7 +452,7 @@ describe("router", () => {
resolve,
);

jest.advanceTimersByTime(131);
vi.advanceTimersByTime(131);
}));

it("expand parameters for named route", () => {
Expand Down Expand Up @@ -607,6 +600,6 @@ describe("router", () => {
resolve,
);

jest.advanceTimersByTime(131);
vi.advanceTimersByTime(131);
}));
});
5 changes: 0 additions & 5 deletions src/test/setup.js

This file was deleted.

10 changes: 5 additions & 5 deletions src/test/widgetTest.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import widget from "../widget.js";
import htmlCanvas from "../htmlCanvas.js";
import jQuery from "jquery";
import { jest, describe, it, expect } from "@jest/globals";
import { vi, describe, it, expect } from "vitest";

const widgetSubclass = widget.subclass((that) => {
that.renderContentOn = function (html) {
Expand Down Expand Up @@ -94,7 +94,7 @@ describe("function", () => {
return that;
})();

let spy = jest.fn();
let spy = vi.fn();

// Assert: that callback is executed when
aWidget.anEvent.register(spy);
Expand Down Expand Up @@ -365,7 +365,7 @@ describe("function", () => {
});

it("widgets initialize their subwidgets", () => {
let spy = jest.fn();
let spy = vi.fn();
let mySubclass = widget.subclass((that, my) => {
my.initializeSubwidgets = spy;
});
Expand All @@ -377,8 +377,8 @@ describe("function", () => {
it("widgets initialize their subwidgets after themselves", () => {
expect.assertions(2);

let init = jest.fn();
let initSub = jest.fn();
let init = vi.fn();
let initSub = vi.fn();

let mySubclass = widget.subclass((that, my) => {
my.initialize = init;
Expand Down
14 changes: 14 additions & 0 deletions vitest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
restoreMocks: true,
environment: "jsdom",
include: ["src/test/**/*Test.js"],

coverage: {
enabled: true,
include: ["src/**", "!src/test/**", "!src/widgetjs.js"],
},
},
});

0 comments on commit a7b3a23

Please sign in to comment.