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

Migrate from jest to vitest (wip) #92

Draft
wants to merge 32 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
48acce5
chore(deps): install vitest
ayushmanchhabra Jun 22, 2024
b18f5b6
chore(ci): upgrade github actions
ayushmanchhabra Jun 22, 2024
1b69e1e
chore(ci): use Node v18.7.0
ayushmanchhabra Jun 22, 2024
123c37f
fix(ci): resolve syntax error
ayushmanchhabra Jun 22, 2024
237f8b8
chore: use Node v18.20.3
ayushmanchhabra Jun 22, 2024
3682301
chore(test): migrate tests from jest to vitest
ayushmanchhabra Jun 22, 2024
7fcf412
chore(ci): use volta action to get old node versions
ayushmanchhabra Jun 22, 2024
ce4703e
chore(test): port jest mocks to vitest
ayushmanchhabra Jun 22, 2024
d650af5
chore(ci): pin node versions using volta
ayushmanchhabra Jun 22, 2024
80ddf84
fix(ci): resolve syntax error
ayushmanchhabra Jun 22, 2024
63d96cf
chore(ci): cancel in progress jobs
ayushmanchhabra Jun 22, 2024
ba038db
fix(ci): resolve syntax error
ayushmanchhabra Jun 22, 2024
374456d
chore(ci): add back some deleted config
ayushmanchhabra Jun 22, 2024
fe9c552
chore(ci): node 18.20.3 -> 18.x
ayushmanchhabra Jun 22, 2024
ff48fdd
fix(ci): npm -v on Node 18 only
ayushmanchhabra Jun 22, 2024
4e83e86
chore(ci): node 8 -> 8.3.0
ayushmanchhabra Jun 22, 2024
8186509
chore(eslint): add back tjw-jest
ayushmanchhabra Jun 23, 2024
4df6457
chore(vite): remove timeout config
ayushmanchhabra Jun 23, 2024
294439d
chore(vitest): enable global apis
ayushmanchhabra Jun 23, 2024
f10dcd8
chore(jest): remove jest workaround comment
ayushmanchhabra Jun 23, 2024
62957ef
chore: revert to original module import style
ayushmanchhabra Jun 23, 2024
91c9bbd
Merge branch 'dev-82' of github.com:nwutils/create-desktop-shortcuts …
ayushmanchhabra Jun 23, 2024
2f480e5
chore(ci): volta pin npm version for Node 8
ayushmanchhabra Jun 23, 2024
95b962a
chore(vitest): mock some node modules
ayushmanchhabra Jun 23, 2024
2fc245f
chore(vitest): misc changes
ayushmanchhabra Jun 23, 2024
6f7300c
chore(deps): update lock file
ayushmanchhabra Jun 23, 2024
b17cf2e
chore(vitest): mock node:os
ayushmanchhabra Jun 25, 2024
ddda8da
chore(test): helper test suite passes
ayushmanchhabra Jun 25, 2024
8e05557
chore(test): jest -> vi
ayushmanchhabra Jun 25, 2024
68f490d
Merge branch 'dev-82' of github.com:nwutils/create-desktop-shortcuts …
ayushmanchhabra Jun 25, 2024
969fdf9
Merge branch 'main' into dev-82
ayushmanchhabra Jun 25, 2024
8d9e777
chore: revert some things
ayushmanchhabra Aug 11, 2024
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
17 changes: 0 additions & 17 deletions jest.config.js

This file was deleted.

9,947 changes: 2,882 additions & 7,065 deletions package-lock.json

Large diffs are not rendered by default.

11 changes: 4 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
"engines": {
"node": ">=4"
},
"volta": {
"node": "18.14.0",
"npm": "9.3.1"
},
"keywords": [
"Desktop Shortcuts",
"desktop icon",
Expand All @@ -31,7 +27,7 @@
"debug": "node --inspect-brk manual-testing.js",
"debug-test": "node --inspect-brk ./node_modules/jest/bin/jest.js --runInBand -t \"powershell\" \"validation\"",
"manual-test": "node manual-testing.js",
"test": "jest --runInBand --coverage",
"test": "vitest run --coverage",
"e2e": "node ./tests/e2e.js",
"validate": "npm run lint && npm test && npm run e2e && git status"
},
Expand All @@ -41,16 +37,17 @@
"devDependencies": {
"@babel/core": "^7.23.7",
"@babel/eslint-parser": "^7.23.3",
"@vitest/coverage-v8": "^1.6.0",
"eslint": "^8.56.0",
"eslint-config-tjw-base": "^2.0.0",
"eslint-config-tjw-jest": "^2.0.0",
"eslint-config-tjw-jsdoc": "^1.0.5",
"eslint-plugin-jsdoc": "^48.0.2",
"fs-extra": "8.1.0",
"get-windows-shortcut-properties": "^1.3.0",
"jest": "24.9.0",
"mock-fs": "^5.2.0",
"path-type": "4.0.0"
"path-type": "4.0.0",
"vitest": "^1.6.0"
},
"license": "MIT",
"repository": {
Expand Down
8 changes: 4 additions & 4 deletions tests/__mocks__/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
* @author TheJaredWilcurt
*/

const childProcess = jest.requireActual('child_process');
const childProcess = await vi.importActual('child_process');

const childProcessMock = Object.assign({}, childProcess, {
execSync: jest.fn((executableAndArgs) => {
execSync: vi.fn((executableAndArgs) => {
if (executableAndArgs.includes('Throw Error')) {
throw 'Successfully errored';
}
Expand All @@ -17,11 +17,11 @@ const childProcessMock = Object.assign({}, childProcess, {
return 'C:/Powershell-derived-desktop';
}
}),
spawnSync: jest.fn((executable, args) => {
spawnSync: vi.fn((executable, args) => {
if (args.includes('Throw Error')) {
throw 'Successfully errored';
}
})
});

module.exports = childProcessMock;
export default childProcessMock;
8 changes: 4 additions & 4 deletions tests/__mocks__/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
* @author TheJaredWilcurt
*/

const fs = jest.requireActual('fs');
const fs = await vi.importActual('fs');

const fsMock = Object.assign({}, fs, {
writeFileSync: jest.fn((file) => {
writeFileSync: vi.fn((file) => {
if (file.includes('Throw Error')) {
throw 'Successfully errored';
}
}),
chmodSync: jest.fn((file) => {
chmodSync: vi.fn((file) => {
if (file.includes('Throw chmod')) {
throw 'Successfully errored';
}
})
});

module.exports = fsMock;
export default fsMock;
6 changes: 4 additions & 2 deletions tests/__mocks__/os.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
* @author TheJaredWilcurt
*/

const os = jest.requireActual('os');
import process from 'process';

const os = await vi.importActual('os');

const osMock = Object.assign({}, os, {
homedir: function () {
Expand All @@ -14,4 +16,4 @@ const osMock = Object.assign({}, os, {
}
});

module.exports = osMock;
export default osMock;
4 changes: 2 additions & 2 deletions tests/__mocks__/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
* @author TheJaredWilcurt
*/

const path = jest.requireActual('path');
const path = await vi.importActual('path');

const pathMock = Object.assign({}, path, {
sep: '/'
});

module.exports = pathMock;
export default pathMock;
6 changes: 3 additions & 3 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
* @author TheJaredWilcurt
*/

const createDesktopShortcut = require('../index.js');
const testHelpers = require('@@/testHelpers.js');
import createDesktopShortcut from '../index.js';
import testHelpers from '@@/testHelpers.js';

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

describe('createDesktopShortcut', () => {
test('Empty options', () => {
Expand Down
26 changes: 12 additions & 14 deletions tests/setup.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
/**
* @file The global setup for all Jest tests.
* @file The global setup for all Vitest tests.
* @author TheJaredWilcurt
*/

const os = require('os');
const processPlatform = process.platform;
const testHelpers = require('@@/testHelpers.js');
import process from 'process';
import os from 'os';
import testHelpers from '@@/testHelpers.js';

if (os.platform() !== 'win32') {
testHelpers.mockOsType();
}

global.beforeEach(() => {
beforeEach(() => {
vi.spyOn(os, 'homedir').mockReturnValue(process.platform === 'win32' ? 'C:\\Users\\DUMMY': '/home/DUMMY');
});

global.afterEach(() => {
testHelpers.mockPlatform(processPlatform);
jest.resetModules();
// thing = jest.fn(); gets called, then .toHaveBeenCalledWith() will see all calls, but this clears the log after each test
jest.clearAllMocks();
afterEach(() => {
testHelpers.mockPlatform(process.platform);
vi.resetModules();
// thing = vi.fn(); gets called, then .toHaveBeenCalledWith() will see all calls, but this clears the log after each test
vi.clearAllMocks();
vi.restoreAllMocks();
});

// Jest's setTimeout defaults to 5 seconds.
// Bump the timeout to 60 seconds.
jest.setTimeout(60 * 1000);
19 changes: 10 additions & 9 deletions tests/src/helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
* @author TheJaredWilcurt
*/

jest.mock('os');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it was related to this, where we mock out the OS module to fake which OS we are on for certain tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some modules are mocked successfully while others aren't such as node:os - will look into it when I get time

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I mocked out one way of checking the OS/platform, and exclusively used that way of checking for it in the library. That way I could pretend to be a different OS in the tests. But also I still needed a way to check the real OS that was running the tests, because it would have different behavior and the tests still needed to pass on all real OS's. So I used a different way to check the real OS that wasn't mocked. I think that's what was going on with process.platform vs require('os').platform().

const processPlatform = process.platform;
vi.mock('os');
import process from 'process';

const helpers = require('@/helpers.js');
import helpers from '@/helpers.js';

const testHelpers = require('@@/testHelpers.js');
import testHelpers from '../testHelpers.js';

describe('helpers', () => {
describe('throwError', () => {
test('Custom logger is called', () => {
const options = {
verbose: true,
customLogger: jest.fn()
customLogger: vi.fn()
};

helpers.throwError(options, 'message', 'error');
Expand All @@ -27,7 +27,7 @@ describe('helpers', () => {
test('Custom logger is not called when verbose is false', () => {
const options = {
verbose: false,
customLogger: jest.fn()
customLogger: vi.fn()
};

helpers.throwError(options, 'message', 'error');
Expand All @@ -38,7 +38,7 @@ describe('helpers', () => {

test('Console.error called when verbose true and no custom logger', () => {
const consoleError = console.error;
console.error = jest.fn();
console.error = vi.fn();
const options = {
verbose: true
};
Expand All @@ -53,12 +53,13 @@ describe('helpers', () => {
});

describe('resolveTilde', () => {

beforeEach(() => {
testHelpers.mockPlatform('linux');
});

afterEach(() => {
testHelpers.mockPlatform(processPlatform);
testHelpers.mockPlatform(process.platform);
});

test('Returns undefined if nothing passed in', () => {
Expand Down Expand Up @@ -103,7 +104,7 @@ describe('helpers', () => {
});

afterEach(() => {
testHelpers.mockPlatform(processPlatform);
testHelpers.mockPlatform(process.platform);
});

test('Returns undefined if nothing passed in', () => {
Expand Down
23 changes: 12 additions & 11 deletions tests/src/library.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
* @author TheJaredWilcurt
*/

jest.mock('child_process');
jest.mock('fs');
jest.mock('path');
jest.mock('os');
vi.mock('child_process');
vi.mock('fs');
vi.mock('path');
vi.mock('os');

const fs = require('fs');
const childProcess = require('child_process');
import fs from 'fs';

const library = require('@/library.js');
const validation = require('@/validation.js');
const testHelpers = require('@@/testHelpers.js');
import childProcess from 'child_process';

import library from '@/library';
import validation from '@/validation.js';
import testHelpers from '@@/testHelpers.js';

const defaults = testHelpers.defaults;
const mockfs = testHelpers.mockfs;
Expand All @@ -23,7 +24,7 @@

describe('library', () => {
beforeEach(() => {
customLogger = jest.fn();
customLogger = vi.fn();
options = {
customLogger
};
Expand Down Expand Up @@ -204,7 +205,7 @@
.not.toHaveBeenCalled();

expect(fs.chmodSync)
.toHaveBeenLastCalledWith('/home/DUMMY/Desktop/file.desktop', '755');

Check failure on line 208 in tests/src/library.test.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

tests/src/library.test.js > library > makeLinuxShortcut > Basic instructions

AssertionError: expected last "chmodSync" call to have been called with [ …(2) ] - Expected: Array [ "/home/DUMMY/Desktop/file.desktop", "755", ] + Received: undefined ❯ tests/src/library.test.js:208:10

expect(fs.writeFileSync)
.toHaveBeenLastCalledWith(
Expand All @@ -225,7 +226,7 @@
options.linux.outputPath = 'Throw Error';

expect(library.makeLinuxShortcut(options))
.toEqual(false);

Check failure on line 229 in tests/src/library.test.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

tests/src/library.test.js > library > makeLinuxShortcut > Error writing file

AssertionError: expected true to deeply equal false - Expected + Received - false + true ❯ tests/src/library.test.js:229:10

expect(customLogger)
.toHaveBeenLastCalledWith(
Expand Down Expand Up @@ -272,7 +273,7 @@
options.linux.outputPath = 'Throw chmod';

expect(library.makeLinuxShortcut(options))
.toEqual(false);

Check failure on line 276 in tests/src/library.test.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

tests/src/library.test.js > library > makeLinuxShortcut > Error setting permissions

AssertionError: expected true to deeply equal false - Expected + Received - false + true ❯ tests/src/library.test.js:276:10

expect(customLogger)
.toHaveBeenLastCalledWith('ERROR attempting to change permisions of Throw chmod', 'Successfully errored');
Expand Down Expand Up @@ -323,7 +324,7 @@
.not.toHaveBeenCalled();

expect(childProcess.spawnSync)
.toHaveBeenLastCalledWith(

Check failure on line 327 in tests/src/library.test.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

tests/src/library.test.js > library > makeWindowsShortcut > Basic instructions

AssertionError: expected last "spawnSync" call to have been called with [ 'wscript', [ …(9) ] ] - Expected: Array [ "wscript", Array [ "/home/runner/work/create-desktop-shortcuts/create-desktop-shortcuts/src/windows.vbs", "C:/Users/DUMMY/Desktop/file.lnk", "C:/file.ext", "", "", "", "C:/file.ext", 1, "", ], ] + Received: undefined ❯ tests/src/library.test.js:327:10
'wscript',
[
library.produceWindowsVBSPath(),
Expand Down Expand Up @@ -352,7 +353,7 @@
.not.toHaveBeenCalled();

expect(childProcess.spawnSync)
.toHaveBeenLastCalledWith(

Check failure on line 356 in tests/src/library.test.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

tests/src/library.test.js > library > makeWindowsShortcut > Use window mode default

AssertionError: expected last "spawnSync" call to have been called with [ 'wscript', [ …(9) ] ] - Expected: Array [ "wscript", Array [ "/home/runner/work/create-desktop-shortcuts/create-desktop-shortcuts/src/windows.vbs", "C:/Users/DUMMY/Desktop/file.lnk", "C:/file.ext", "", "", "", "C:/file.ext", 1, "", ], ] + Received: undefined ❯ tests/src/library.test.js:356:10
'wscript',
[
library.produceWindowsVBSPath(),
Expand Down Expand Up @@ -381,7 +382,7 @@
.not.toHaveBeenCalled();

expect(childProcess.spawnSync)
.toHaveBeenLastCalledWith(

Check failure on line 385 in tests/src/library.test.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

tests/src/library.test.js > library > makeWindowsShortcut > Icon

AssertionError: expected last "spawnSync" call to have been called with [ 'wscript', [ …(9) ] ] - Expected: Array [ "wscript", Array [ "/home/runner/work/create-desktop-shortcuts/create-desktop-shortcuts/src/windows.vbs", "C:/Users/DUMMY/Desktop/file.lnk", "C:/file.ext", "", "", "", "C:/Users/DUMMY/icon.ico", 1, "", ], ] + Received: undefined ❯ tests/src/library.test.js:385:10
'wscript',
[
library.produceWindowsVBSPath(),
Expand Down Expand Up @@ -411,7 +412,7 @@
.not.toHaveBeenCalled();

expect(childProcess.spawnSync)
.toHaveBeenLastCalledWith(

Check failure on line 415 in tests/src/library.test.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

tests/src/library.test.js > library > makeWindowsShortcut > No icon

AssertionError: expected last "spawnSync" call to have been called with [ 'wscript', [ …(9) ] ] - Expected: Array [ "wscript", Array [ "/home/runner/work/create-desktop-shortcuts/create-desktop-shortcuts/src/windows.vbs", "C:/Users/DUMMY/Desktop/file.lnk", "C:/Users/DUMMY/icon.dll", "", "", "", "C:/Users/DUMMY/icon.dll,0", 1, "", ], ] + Received: undefined ❯ tests/src/library.test.js:415:10
'wscript',
[
library.produceWindowsVBSPath(),
Expand Down Expand Up @@ -440,7 +441,7 @@
.not.toHaveBeenCalled();

expect(childProcess.spawnSync)
.toHaveBeenLastCalledWith(

Check failure on line 444 in tests/src/library.test.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

tests/src/library.test.js > library > makeWindowsShortcut > Windows arguments

AssertionError: expected last "spawnSync" call to have been called with [ 'wscript', [ …(9) ] ] - Expected: Array [ "wscript", Array [ "/home/runner/work/create-desktop-shortcuts/create-desktop-shortcuts/src/windows.vbs", "C:/Users/DUMMY/Desktop/file.lnk", "C:/file.ext", "--force", "", "", "C:/file.ext", 1, "", ], ] + Received: undefined ❯ tests/src/library.test.js:444:10
'wscript',
[
library.produceWindowsVBSPath(),
Expand Down Expand Up @@ -469,7 +470,7 @@
.not.toHaveBeenCalled();

expect(childProcess.spawnSync)
.toHaveBeenLastCalledWith(

Check failure on line 473 in tests/src/library.test.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

tests/src/library.test.js > library > makeWindowsShortcut > Windows arguments contains double quotes

AssertionError: expected last "spawnSync" call to have been called with [ 'wscript', [ …(9) ] ] - Expected: Array [ "wscript", Array [ "/home/runner/work/create-desktop-shortcuts/create-desktop-shortcuts/src/windows.vbs", "C:/Users/DUMMY/Desktop/file.lnk", "C:/file.ext", "-m __DOUBLEQUOTE__Some text__DOUBLEQUOTE__", "", "", "C:/file.ext", 1, "", ], ] + Received: undefined ❯ tests/src/library.test.js:473:10
'wscript',
[
library.produceWindowsVBSPath(),
Expand Down Expand Up @@ -498,7 +499,7 @@
.not.toHaveBeenCalled();

expect(childProcess.spawnSync)
.toHaveBeenLastCalledWith(

Check failure on line 502 in tests/src/library.test.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

tests/src/library.test.js > library > makeWindowsShortcut > Windows comment contains double quotes

AssertionError: expected last "spawnSync" call to have been called with [ 'wscript', [ …(9) ] ] - Expected: Array [ "wscript", Array [ "/home/runner/work/create-desktop-shortcuts/create-desktop-shortcuts/src/windows.vbs", "C:/Users/DUMMY/Desktop/file.lnk", "C:/file.ext", "", "Look at what __DOUBLEQUOTE__I__DOUBLEQUOTE__ made.", "", "C:/file.ext", 1, "", ], ] + Received: undefined ❯ tests/src/library.test.js:502:10
'wscript',
[
library.produceWindowsVBSPath(),
Expand Down Expand Up @@ -545,7 +546,7 @@

test('Windows.vbs not found', () => {
const fsExistsSync = fs.existsSync;
fs.existsSync = jest.fn(() => {
fs.existsSync = vi.fn(() => {
return false;
});

Expand Down
16 changes: 8 additions & 8 deletions tests/src/validation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
* @author TheJaredWilcurt
*/

jest.mock('child_process');
jest.mock('path');
jest.mock('os');
vi.mock('child_process');
vi.mock('path');
vi.mock('os');

const os = require('os');
import os from 'os';

const validation = require('@/validation.js');
const testHelpers = require('@@/testHelpers.js');
import validation from '@/validation';
import testHelpers from '@@/testHelpers';

const defaults = testHelpers.defaults;
const mockfs = testHelpers.mockfs;
Expand All @@ -20,7 +20,7 @@ let customLogger;

describe('Validation', () => {
beforeEach(() => {
customLogger = jest.fn();
customLogger = vi.fn();
});

afterEach(() => {
Expand Down Expand Up @@ -55,7 +55,7 @@ describe('Validation', () => {

test('Defaults', () => {
const consoleError = console.error;
console.error = jest.fn();
console.error = vi.fn();

options = {
customLogger: 'Not a function'
Expand Down
12 changes: 5 additions & 7 deletions tests/testHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
* @author TheJaredWilcurt
*/

const path = require('path');
const os = require('os');
const mock = require('mock-fs');
import os from 'os';
import path from 'path';
import mock from 'mock-fs';

const testHelpers = {
/**
Expand Down Expand Up @@ -55,9 +55,7 @@ const testHelpers = {
type = type || 'cygwin';
if (process && process.env) {
this.OSTYPE = process.env.OSTYPE;
Object.defineProperty(process.env, 'OSTYPE', {
value: type
});
process.env.OSTYPE = type;
}
},
/**
Expand Down Expand Up @@ -241,4 +239,4 @@ const testHelpers = {
}
};

module.exports = testHelpers;
export default testHelpers;
20 changes: 20 additions & 0 deletions vitest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { fileURLToPath, URL } from 'url';

import { defineConfig } from 'vitest/config'

export default defineConfig
({
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)),
'@@': fileURLToPath(new URL('./tests', import.meta.url)),
}
},
test: {
coverage: {
provider: 'v8',
},
globals: true,
setupFiles: './tests/setup.js',
},
});
Loading