Skip to content

Commit 2ecfa12

Browse files
committed
feat: Install and configure jest
1 parent a153846 commit 2ecfa12

File tree

6 files changed

+120
-2
lines changed

6 files changed

+120
-2
lines changed

.eslintrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
module.exports = {
22
env: {
33
browser: true,
4-
es6: true
4+
es6: true,
5+
jest: true
56
},
67
extends: [
78
'standard'

babel.test.config.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module.exports = {
2+
plugins: [
3+
["@babel/plugin-transform-modules-commonjs", {
4+
importInterop: 'babel'
5+
}]
6+
],
7+
presets: [
8+
"@babel/preset-env",
9+
"@babel/preset-typescript"
10+
]
11+
};

browserMock.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const fs = require('fs/promises')
2+
3+
Object.defineProperty(document, 'queryCommandSupported', {
4+
value: jest.fn().mockImplementation(() => true),
5+
});
6+
7+
Object.defineProperty(window, 'matchMedia', {
8+
writable: true,
9+
value: jest.fn().mockImplementation(query => ({
10+
matches: false,
11+
media: query,
12+
onchange: null,
13+
addListener: jest.fn(), // Deprecated
14+
removeListener: jest.fn(), // Deprecated
15+
addEventListener: jest.fn(),
16+
removeEventListener: jest.fn(),
17+
dispatchEvent: jest.fn(),
18+
})),
19+
});
20+
21+
Object.defineProperty(window, 'fetch', {
22+
value: jest.fn(async (path) => {
23+
24+
const content = await fs.readFile(path)
25+
return {
26+
json: async () => JSON.stringify(JSON.parse(content.toString())),
27+
arrayBuffer: async () => content.buffer.slice(content.byteOffset, content.byteOffset + content.byteLength)
28+
}
29+
})
30+
})
31+
32+
Object.defineProperty(URL, 'createObjectURL', {
33+
value: jest.fn((blob) => {
34+
35+
return null
36+
})
37+
})
38+
39+
Object.defineProperty(window, 'Worker', {
40+
value: class Worker {
41+
constructor(stringUrl) {}
42+
postMessage(msg) {}
43+
terminate () {}
44+
}
45+
})
46+
47+
Object.defineProperty(window, 'ResizeObserver', {
48+
value: class ResizeObserver {
49+
constructor(stringUrl) {}
50+
observe() {}
51+
}
52+
})

jest/cssTransform.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
// This is a custom Jest transformer turning style imports into empty objects.
4+
// http://facebook.github.io/jest/docs/en/webpack.html
5+
6+
module.exports = {
7+
process() {
8+
return 'module.exports = {};';
9+
},
10+
getCacheKey() {
11+
// The output is always the same.
12+
return 'cssTransform';
13+
},
14+
};

jest/fileTransform.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
process(src, filename) {
3+
const assetFilename = JSON.stringify(filename);
4+
5+
return `module.exports = ${assetFilename};`;
6+
},
7+
};

package.json

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"private": false,
55
"description": "Enhanced Monaco editor with TextMate grammars and more",
66
"scripts": {
7-
"build": "rollup --config rollup.config.ts --configPlugin typescript && npm run generate-types",
7+
"test": "npx jest",
8+
"test:watch": "jest --watch",
9+
"build": "npm run test && rollup --config rollup.config.ts --configPlugin typescript && npm run generate-types",
810
"prepublishOnly": "npm run build",
911
"generate-types": "tsc --project tsconfig.types.json && rollup --config rollup.types.config.ts --configPlugin typescript && rm -rf ./dist/types"
1012
},
@@ -31,6 +33,7 @@
3133
"@babel/core": "7.17.7",
3234
"@babel/plugin-proposal-class-properties": "7.16.7",
3335
"@babel/plugin-proposal-optional-chaining": "7.16.7",
36+
"@babel/plugin-transform-modules-commonjs": "^7.17.7",
3437
"@babel/preset-env": "7.16.11",
3538
"@babel/preset-typescript": "7.16.7",
3639
"@babel/runtime": "7.17.7",
@@ -41,6 +44,7 @@
4144
"@rollup/plugin-json": "4.1.0",
4245
"@rollup/plugin-node-resolve": "13.1.3",
4346
"@rollup/plugin-typescript": "^8.3.1",
47+
"@types/jest": "^27.4.1",
4448
"@types/once": "^1.4.0",
4549
"@types/rollup-plugin-node-builtins": "^2.1.2",
4650
"@types/vscode": "^1.65.0",
@@ -51,9 +55,11 @@
5155
"eslint-config-standard": "17.0.0-1",
5256
"eslint-config-standard-jsx": "11.0.0-1",
5357
"eslint-plugin-import": "2.25.4",
58+
"eslint-plugin-jest": "^26.1.1",
5459
"eslint-plugin-node": "11.1.0",
5560
"eslint-plugin-promise": "6.0.0",
5661
"eslint-plugin-unused-imports": "2.0.0",
62+
"jest": "^27.5.1",
5763
"monaco-languageclient": "^0.18.0",
5864
"proxy-polyfill": "^0.3.2",
5965
"rollup": "2.70.1",
@@ -76,6 +82,33 @@
7682
"not IE 11",
7783
"not IE_Mob 11"
7884
],
85+
"jest": {
86+
"testEnvironment": "jest-environment-jsdom",
87+
"modulePathIgnorePatterns": [
88+
"<rootDir>/dist/"
89+
],
90+
"setupFiles": [
91+
"<rootDir>/browserMock.js"
92+
],
93+
"transformIgnorePatterns": [
94+
"^.+\\.module\\.(css|sass|scss)$"
95+
],
96+
"transform": {
97+
"^(?!.*\\.(js|ts|css|json)$)": "<rootDir>/jest/fileTransform.js",
98+
"^.+\\.css$": "<rootDir>/jest/cssTransform.js",
99+
"^.+\\.(js|ts)$": [
100+
"<rootDir>/node_modules/babel-jest",
101+
{
102+
"configFile": "./babel.test.config.js"
103+
}
104+
]
105+
},
106+
"moduleNameMapper": {
107+
"^vscode$": "monaco-languageclient/lib/vscode-compatibility",
108+
"^monaco-editor$": "monaco-editor/esm/vs/editor/edcore.main",
109+
"^monaco-editor-core$": "monaco-editor/esm/vs/editor/edcore.main"
110+
}
111+
},
79112
"volta": {
80113
"node": "16.14.0",
81114
"npm": "8.5.0"

0 commit comments

Comments
 (0)