Skip to content

Commit

Permalink
feat(vitest): replace jest with vitest and removes babel
Browse files Browse the repository at this point in the history
  • Loading branch information
BenTopping committed Feb 5, 2025
1 parent 8501436 commit e7069c3
Show file tree
Hide file tree
Showing 14 changed files with 955 additions and 4,243 deletions.
4 changes: 1 addition & 3 deletions app/frontend/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ module.exports = {
browser: true,
es6: true,
amd: true,
"jest/globals": true,
node: true,
},
plugins: ["vue", "jest"],
plugins: ["vue"],
extends: ["eslint:recommended", "plugin:vue/recommended", "prettier"],
parserOptions: {
parser: "babel-eslint",
sourceType: "module",
ecmaVersion: 2018,
},
Expand Down
1 change: 0 additions & 1 deletion app/frontend/plate-picks/components/HelpToggle.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { shallowMount } from "@vue/test-utils";
import "@testing-library/jest-dom";
import HelpToggle from "./HelpToggle.vue";

describe("HelpToggle.vue", () => {

Check failure on line 4 in app/frontend/plate-picks/components/HelpToggle.test.js

View workflow job for this annotation

GitHub Actions / EsLint

'describe' is not defined

Check failure on line 4 in app/frontend/plate-picks/components/HelpToggle.test.js

View workflow job for this annotation

GitHub Actions / EsLint

'describe' is not defined
Expand Down
1 change: 0 additions & 1 deletion app/frontend/plate-picks/components/PicksSection.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { shallowMount } from "@vue/test-utils";
import "@testing-library/jest-dom";
import PicksSection from "./PicksSection.vue";
import PicksSectionBatch from "./PicksSectionBatch.vue";
import defaultState from "../store/state";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { shallowMount } from "@vue/test-utils";
import "@testing-library/jest-dom";
import PicksSectionBatch from "./PicksSectionBatch.vue";
import PicksSectionBatchPick from "./PicksSectionBatchPick.vue";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { shallowMount } from "@vue/test-utils";
import "@testing-library/jest-dom";
import PicksSectionBatchPick from "./PicksSectionBatchPick.vue";
import defaultState from "../store/state";
import { storePlateWithPicks } from "../_test_examples_";
Expand Down Expand Up @@ -81,7 +80,7 @@ describe("PicksSectionBatchPick.vue", () => {
});

it("includes an editable short name", async () => {
const commit = jest.fn();
const commit = vi.fn();
const wrapper = shallowMount(PicksSectionBatchPick, {
propsData: {
name,
Expand Down
5 changes: 2 additions & 3 deletions app/frontend/plate-picks/components/PlatesSection.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { shallowMount } from "@vue/test-utils";
import "@testing-library/jest-dom";
import PlatesSection from "./PlatesSection.vue";
import PlatesSectionPlate from "./PlatesSectionPlate.vue";
import defaultState from "../store/state";
Expand Down Expand Up @@ -33,7 +32,7 @@ describe("PlatesSection.vue", () => {
it("triggers plate lookup", async () => {
const mockStore = {
state: defaultState(),
dispatch: jest.fn(),
dispatch: vi.fn(),
getters: { sortedScannedPlates: [] },
};
const wrapper = shallowMount(PlatesSection, {
Expand All @@ -52,7 +51,7 @@ describe("PlatesSection.vue", () => {
it("ignores empty input", async () => {
const mockStore = {
state: defaultState(),
dispatch: jest.fn(),
dispatch: vi.fn(),
getters: { sortedScannedPlates: [] },
};
const wrapper = shallowMount(PlatesSection, {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { shallowMount } from "@vue/test-utils";
import "@testing-library/jest-dom";
import PlatesSectionPlate from "./PlatesSectionPlate.vue";
import defaultState from "../store/state";

Expand Down
26 changes: 13 additions & 13 deletions app/frontend/plate-picks/store/actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ describe("actions.js", () => {
// mock commit
const plates = { 1: plateWithPicks() };
const state = { ...defaultState(), plates };
const dispatch = jest.fn();
const commit = jest.fn((_) => (state.scanStore[`_${exampleBarcode}`] = { barcode: exampleBarcode, id: 1 }));
const dispatch = vi.fn();
const commit = vi.fn((_) => (state.scanStore[`_${exampleBarcode}`] = { barcode: exampleBarcode, id: 1 }));
// apply action
await plateBarcodeScan({ commit, state, dispatch }, exampleBarcode);
// assert result
Expand All @@ -36,8 +36,8 @@ describe("actions.js", () => {
mirageServer.create("plate", plateWithoutPicks({ id: "1" }));
// mock commit
const state = defaultState();
const commit = jest.fn((_) => (state.scanStore[`_${exampleBarcode}`] = { barcode: exampleBarcode }));
const dispatch = jest.fn();
const commit = vi.fn((_) => (state.scanStore[`_${exampleBarcode}`] = { barcode: exampleBarcode }));
const dispatch = vi.fn();
// apply action
await plateBarcodeScan({ commit, state, dispatch }, exampleBarcode);
// assert result
Expand All @@ -53,8 +53,8 @@ describe("actions.js", () => {
mirageServer.create("plate", plateWithoutPicks({ id: "1", control: true }));
// mock commit
const state = defaultState();
const commit = jest.fn((_) => (state.scanStore[`_${exampleBarcode}`] = { barcode: exampleBarcode }));
const dispatch = jest.fn();
const commit = vi.fn((_) => (state.scanStore[`_${exampleBarcode}`] = { barcode: exampleBarcode }));
const dispatch = vi.fn();
// apply action
await plateBarcodeScan({ commit, state, dispatch }, exampleBarcode);
// assert result
Expand All @@ -73,10 +73,10 @@ describe("actions.js", () => {
// mock the console
// We don't *need* to do this, but it clutters the output and confuses
// actual failing tests
console.error = jest.fn();
console.error = vi.fn();
// mock commit
const state = defaultState();
const commit = jest.fn((_) => (state.scanStore["_BadPlate"] = { barcode: "BadPlate" }));
const commit = vi.fn((_) => (state.scanStore["_BadPlate"] = { barcode: "BadPlate" }));
// mock dependencies
// apply action
await plateBarcodeScan({ commit, state }, "BadPlate");
Expand All @@ -93,8 +93,8 @@ describe("actions.js", () => {
describe("fetchBatches", () => {
it("fetches each batch`", () => {
// mock commit
const commit = jest.fn();
const dispatch = jest.fn();
const commit = vi.fn();
const dispatch = vi.fn();
const state = defaultState();
// apply action
fetchBatches({ commit, dispatch, state }, { ids: [1, 2] });
Expand Down Expand Up @@ -132,8 +132,8 @@ describe("actions.js", () => {

// mock commit
const state = defaultState();
const commit = jest.fn().mockImplementationOnce((_) => (state.batches["b1"] = { id: "1" }));
const dispatch = jest.fn((_) => (state.pickCount += 1));
const commit = vi.fn().mockImplementationOnce((_) => (state.batches["b1"] = { id: "1" }));
const dispatch = vi.fn((_) => (state.pickCount += 1));

// apply action
await fetchBatch({ commit, dispatch, state }, { id: "1" });
Expand Down Expand Up @@ -170,7 +170,7 @@ describe("actions.js", () => {
// Set up initial state
const state = defaultState();
// apply action
const commit = jest.fn((_) => (state.pickCount += 1));
const commit = vi.fn((_) => (state.pickCount += 1));
const returned = nextPickId({ commit, state });
expect(commit).toHaveBeenCalledWith("incrementPick");
expect(returned).toBe(1);
Expand Down
4 changes: 2 additions & 2 deletions app/frontend/plate-picks/store/mutations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe("mutations.js", () => {
it("updates plates when they exist already", () => {
// Provided by the browser. We mock it. Currently just mock a string as we're not doing anything fancy
global.SpeechSynthesisUtterance = class extends String {};
global.speechSynthesis = { speak: jest.fn() };
global.speechSynthesis = { speak: vi.fn() };
// mock state
const state = defaultState();
state.plates = { 1: plateWithoutPicks({ id: 1 }) };
Expand All @@ -64,7 +64,7 @@ describe("mutations.js", () => {
it("updates picks when they exist already", () => {
// Provided by the browser. We mock it. Currently just mock a string as we're not doing anything fancy
global.SpeechSynthesisUtterance = class extends String {};
global.speechSynthesis = { speak: jest.fn() };
global.speechSynthesis = { speak: vi.fn() };
// mock state
const state = {
...defaultState(),
Expand Down
1 change: 0 additions & 1 deletion app/frontend/shared/components/BaseRadio.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { shallowMount } from "@vue/test-utils";
import "@testing-library/jest-dom";
import BaseRadio from "./BaseRadio.vue";

describe("BaseRadio.vue", () => {
Expand Down
41 changes: 0 additions & 41 deletions babel.config.js

This file was deleted.

28 changes: 4 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,20 @@
"whatwg-fetch": "^3.6.20"
},
"devDependencies": {
"@babel/core": "^7.24.7",
"@babel/preset-env": "^7.24.7",
"@prettier/plugin-ruby": "^4.0.0",
"@testing-library/jest-dom": "^5.17.0",
"@vitejs/plugin-legacy": "^1.8.2",
"@vue/test-utils": "^1.3.6",
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "^10.1.0",
"babel-jest": "^26.6.3",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.10.0",
"eslint-plugin-jest": "^23.20.0",
"eslint-plugin-vitest": "^0.5.4",
"eslint-plugin-vue": "^6.2.2",
"jest": "^26.6.3",
"jsdom": "^26.0.0",
"miragejs": "^0.1.48",
"prettier": "^3.3.2",
"vite": "^5.4.14",
"vite-plugin-ruby": "^5.0.0",
"vite-plugin-vue2": "^1.9.3",
"vue-jest": "^3.0.7"
"vitest": "^3.0.5"
},
"_comment": "Required to address https://github.com/npm/cli/issues/4828",
"optionalDependencies": {
Expand All @@ -59,22 +53,8 @@
"@rollup/rollup-win32-x64-msvc": "*"
},
"scripts": {
"test": "jest",
"test": "vitest run",
"lint": "eslint --ext .js --ext .vue app/frontend",
"lintOnly": "eslint --ext .js --ext .vue"
},
"jest": {
"roots": [
"app/frontend"
],
"moduleFileExtensions": [
"js",
"json",
"vue"
],
"transform": {
"^.+\\.(vue)$": "vue-jest",
"^.+\\.js$": "babel-jest"
}
}
}
12 changes: 12 additions & 0 deletions vite.config.ts → vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,16 @@ export default defineConfig({
"@formtastic": process.env.FORMTASTIC_STYLESHEET_PATH,
},
},
test: {
globals: true,
environment: 'jsdom',
coverage: {
provider: 'v8',
reporter: ['lcov', 'text'],
},
// This hides the "Download the Vue Devtools extension" message from the console
onConsoleLog(log) {
if (log.includes('Download the Vue Devtools extension')) return false
},
},
});
Loading

0 comments on commit e7069c3

Please sign in to comment.