Skip to content

Commit 48798f1

Browse files
egardnerMusikAnimal
authored andcommitted
Add Jest to MW Core for testing Vue components
This brings Jest to MW Core so that Vue components can more easily be tested. Packages ending with 'icons.json' are auto-mapped to the @wikimedia/codex-icons package, establishing a new convention. Jest is needed for testing Vue, but can be used for general JS testing as well. Documentation to come at https://www.mediawiki.org/wiki/Jest Usage: to run the tests, run "npx jest" or "npm run jest" in the root folder. This could be aliased to a "test:unit" script in package.json if desired. In order to make a Vue component testable, it is required to follow the `module.exports = exports` pattern. See: https://nodejs.org/api/modules.html#exports-shortcut A default jest.setup.js is added that mocks Core's mw global object, as well as providing the $i18n Vue plugin. This is based on Extension:GrowthExperiments (GPL-3.0-or-later). See https://w.wiki/B9vM An initial test for mediawiki.special.block is added, exemplifying how to mock calls to mw.config.get(). NOTE: The "overrides" added to package.json is a temporary measure to get around a current issue with caniuse-lite and the stylelint plugin that relies on it. See: RJWadley/stylelint-no-unsupported-browser-features#299 Bug: T251790 Change-Id: I1af15e038d17b0f1c645fc5debf9db5846f6215c
1 parent 5319d0b commit 48798f1

14 files changed

+22427
-10748
lines changed

package-lock.json

+22,074-10,739
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+13-1
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,21 @@
1010
"qunit": "grunt qunit",
1111
"selenium-daily": "npm run selenium-test -- --mochaOpts.grep @daily",
1212
"selenium-test": "wdio ./tests/selenium/wdio.conf.js",
13-
"test": "grunt lint && npm run doc"
13+
"test": "grunt lint && npm run doc && npm run jest",
14+
"jest": "jest --config tests/jest/jest.config.js"
1415
},
1516
"devDependencies": {
1617
"@apidevtools/swagger-parser": "^10.1.0",
18+
"@babel/preset-env": "7.25.4",
19+
"@vue/test-utils": "2.4.6",
20+
"@vue/vue3-jest": "29.2.6",
1721
"@wdio/cli": "7.33.0",
1822
"@wdio/junit-reporter": "7.33.0",
1923
"@wdio/local-runner": "7.33.0",
2024
"@wdio/mocha-framework": "7.33.0",
2125
"@wdio/spec-reporter": "7.33.0",
26+
"@wikimedia/codex": "1.12.0",
27+
"@wikimedia/codex-icons": "1.12.0",
2228
"@wikimedia/karma-firefox-launcher": "2.1.3",
2329
"api-testing": "1.6.0",
2430
"domino": "2.1.0",
@@ -30,6 +36,8 @@
3036
"grunt-eslint": "24.3.0",
3137
"grunt-karma": "4.0.2",
3238
"grunt-stylelint": "0.20.0",
39+
"jest": "29.7.0",
40+
"jest-environment-jsdom": "29.7.0",
3341
"jsdoc": "4.0.2",
3442
"jsdoc-wmf-theme": "1.1.0",
3543
"karma": "6.4.1",
@@ -39,7 +47,11 @@
3947
"qunit": "2.20.0",
4048
"stylelint-config-wikimedia": "0.17.2",
4149
"svgo": "3.2.0",
50+
"vue": "3.4.27",
4251
"wdio-mediawiki": "file:tests/selenium/wdio-mediawiki",
4352
"xml2js": "^0.6.2"
53+
},
54+
"overrides": {
55+
"caniuse-lite": "1.0.30001653"
4456
}
4557
}

resources/src/mediawiki.special.block/SpecialBlock.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const ReasonField = require( './components/ReasonField.vue' );
4242
const BlockDetailsField = require( './components/BlockDetailsOptions.vue' );
4343
4444
// @vue/component
45-
module.exports = defineComponent( {
45+
module.exports = exports = defineComponent( {
4646
name: 'SpecialBlock',
4747
components: {
4848
UserLookup,

resources/src/mediawiki.special.block/components/BlockDetailsOptions.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const { defineComponent, toRef } = require( 'vue' );
2626
const { CdxCheckbox, CdxField, useModelWrapper } = require( '@wikimedia/codex' );
2727
2828
// @vue/component
29-
module.exports = defineComponent( {
29+
module.exports = exports = defineComponent( {
3030
name: 'BlockDetailsField',
3131
components: { CdxCheckbox, CdxField },
3232
props: {

resources/src/mediawiki.special.block/components/BlockTypeField.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const { defineComponent, ref, toRef } = require( 'vue' );
3939
const { CdxCheckbox, CdxRadio, CdxField, useModelWrapper } = require( '@wikimedia/codex' );
4040
4141
// @vue/component
42-
module.exports = defineComponent( {
42+
module.exports = exports = defineComponent( {
4343
name: 'BlockTypeField',
4444
components: { CdxCheckbox, CdxRadio, CdxField },
4545
props: {

resources/src/mediawiki.special.block/components/ExpiryField.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
const { defineComponent, ref, computed, watch } = require( 'vue' );
6262
const { CdxField, CdxRadio, CdxSelect, CdxTextInput } = require( '@wikimedia/codex' );
6363
64-
module.exports = defineComponent( {
64+
module.exports = exports = defineComponent( {
6565
name: 'ExpiryField',
6666
components: {
6767
CdxField,

resources/src/mediawiki.special.block/components/ReasonField.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
const { defineComponent } = require( 'vue' );
99
1010
// @vue/component
11-
module.exports = defineComponent( {
11+
module.exports = exports = defineComponent( {
1212
name: 'ReasonField'
1313
} );
1414
</script>

resources/src/mediawiki.special.block/components/TargetActiveBlocks.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
const { defineComponent } = require( 'vue' );
99
1010
// @vue/component
11-
module.exports = defineComponent( {
11+
module.exports = exports = defineComponent( {
1212
name: 'TargetActiveBlocks'
1313
} );
1414
</script>

resources/src/mediawiki.special.block/components/TargetBlockLog.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
const { defineComponent } = require( 'vue' );
99
1010
// @vue/component
11-
module.exports = defineComponent( {
11+
module.exports = exports = defineComponent( {
1212
name: 'TargetBlockLog'
1313
} );
1414
</script>

resources/src/mediawiki.special.block/components/UserLookup.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const { CdxLookup, CdxField, useModelWrapper } = require( '@wikimedia/codex' );
2424
const { cdxIconSearch } = require( '../icons.json' );
2525
2626
// @vue/component
27-
module.exports = defineComponent( {
27+
module.exports = exports = defineComponent( {
2828
name: 'UserLookup',
2929
components: { CdxLookup, CdxField },
3030
props: {

tests/jest/.eslintrc.json

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"root": true,
3+
"extends": [
4+
"wikimedia/client-common",
5+
"wikimedia/language/es2020",
6+
"wikimedia/jquery",
7+
"wikimedia/mediawiki"
8+
],
9+
"parserOptions": {
10+
"sourceType": "module"
11+
},
12+
"env": {
13+
"browser": true,
14+
"jest": true
15+
},
16+
"globals": {
17+
"global": "writable"
18+
},
19+
"rules": {
20+
"compat/compat": [ "error", "last 2 chrome versions, last 2 firefox versions" ],
21+
"max-len": "off"
22+
}
23+
}

tests/jest/jest.config.js

+204
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
'use strict';
2+
3+
/*
4+
* For a detailed explanation regarding each configuration property, visit:
5+
* https://jestjs.io/docs/configuration
6+
*/
7+
module.exports = {
8+
// All imported modules in your tests should be mocked automatically
9+
// automock: false,
10+
11+
// Stop running tests after `n` failures
12+
// bail: 0,
13+
14+
// The directory where Jest should store its cached dependency information
15+
// cacheDirectory: '/private/var/folders/4c/2pxczd5n1c7_7z_s5djxfx5w0000gn/T/jest_dx',
16+
17+
// Automatically clear mock calls, instances and results before every test
18+
clearMocks: true,
19+
20+
// Indicates whether the coverage information should be collected while executing the test
21+
collectCoverage: false,
22+
23+
// An array of glob patterns indicating a set of files for which coverage information should be
24+
// collected
25+
// collectCoverageFrom: undefined,
26+
27+
// The directory where Jest should output its coverage files
28+
coverageDirectory: 'coverage',
29+
30+
// An array of regexp pattern strings used to skip coverage collection
31+
// coveragePathIgnorePatterns: [
32+
// '/node_modules/'
33+
// ],
34+
35+
// Indicates which provider should be used to instrument code for coverage
36+
// coverageProvider: "babel",
37+
38+
// A list of reporter names that Jest uses when writing coverage reports
39+
// coverageReporters: [
40+
// 'json',
41+
// 'text',
42+
// 'lcov',
43+
// 'clover'
44+
// ],
45+
46+
// An object that configures minimum threshold enforcement for coverage results
47+
// coverageThreshold: undefined,
48+
49+
// A path to a custom dependency extractor
50+
// dependencyExtractor: undefined,
51+
52+
// Make calling deprecated APIs throw helpful error messages
53+
// errorOnDeprecated: false,
54+
55+
// Force coverage collection from ignored files using an array of glob patterns
56+
// forceCoverageMatch: [],
57+
58+
// A path to a module which exports an async function that is triggered once before all test suites
59+
// globalSetup: undefined,
60+
61+
// A path to a module which exports an async function that is triggered once after all test suites
62+
// globalTeardown: undefined,
63+
64+
// A set of global variables that need to be available in all test environments
65+
// globals: {},
66+
67+
// The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
68+
// maxWorkers: '50%',
69+
70+
// An array of directory names to be searched recursively up from the requiring module's location
71+
// moduleDirectories: [
72+
// 'node_modules'
73+
// ],
74+
75+
// An array of file extensions your modules use
76+
moduleFileExtensions: [
77+
'js',
78+
'json',
79+
'vue'
80+
],
81+
82+
// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
83+
moduleNameMapper: {
84+
'icons.json$': '@wikimedia/codex-icons'
85+
},
86+
87+
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
88+
// modulePathIgnorePatterns: [],
89+
90+
// Activates notifications for test results
91+
// notify: false,
92+
93+
// An enum that specifies notification mode. Requires { notify: true }
94+
// notifyMode: 'failure-change',
95+
96+
// A preset that is used as a base for Jest's configuration
97+
// preset: undefined,
98+
99+
// Run tests from one or more projects
100+
// projects: undefined,
101+
102+
// Use this configuration option to add custom reporters to Jest
103+
// reporters: undefined,
104+
105+
// Automatically reset mock state before every test
106+
// resetMocks: false,
107+
108+
// Reset the module registry before running each individual test
109+
// resetModules: false,
110+
111+
// A path to a custom resolver
112+
// resolver: undefined,
113+
114+
// Automatically restore mock state and implementation before every test
115+
// restoreMocks: false,
116+
117+
// The root directory that Jest should scan for tests and modules within
118+
rootDir: '../../',
119+
120+
// A list of paths to directories that Jest should use to search for files in
121+
// roots: [
122+
// '<rootDir>'
123+
// ],
124+
125+
// Allows you to use a custom runner instead of Jest's default test runner
126+
// runner: 'jest-runner',
127+
128+
// The paths to modules that run some code to configure or set up the testing environment before each test
129+
setupFiles: [
130+
'<rootDir>/tests/jest/jest.setup.js'
131+
],
132+
133+
// A list of paths to modules that run some code to configure or set up the testing framework before each test
134+
// setupFilesAfterEnv: [],
135+
136+
// The number of seconds after which a test is considered as slow and reported as such in the results.
137+
// slowTestThreshold: 5,
138+
139+
// A list of paths to snapshot serializer modules Jest should use for snapshot testing
140+
// snapshotSerializers: [],
141+
142+
// The test environment that will be used for testing
143+
testEnvironment: 'jsdom',
144+
145+
// Options that will be passed to the testEnvironment
146+
testEnvironmentOptions: {
147+
customExportConditions: [ 'node', 'node-addons' ]
148+
},
149+
150+
// Adds a location field to test results
151+
// testLocationInResults: false,
152+
153+
// The glob patterns Jest uses to detect test files
154+
testMatch: [
155+
'<rootDir>/tests/jest/**/*.test.js'
156+
],
157+
158+
// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
159+
testPathIgnorePatterns: [
160+
'/node_modules/',
161+
'/vendor/',
162+
'/skins/',
163+
'/extensions/',
164+
'/tests/qunit/'
165+
],
166+
167+
// The regexp pattern or array of patterns that Jest uses to detect test files
168+
// testRegex: [],
169+
170+
// This option allows the use of a custom results processor
171+
// testResultsProcessor: undefined,
172+
173+
// This option allows use of a custom test runner
174+
// testRunner: 'jest-circus/runner',
175+
176+
// This option sets the URL for the jsdom environment. It is reflected in properties such as location.href
177+
// testURL: 'http://localhost',
178+
179+
// Setting this value to "fake" allows the use of fake timers for functions such as "setTimeout"
180+
// timers: 'real',
181+
182+
// A map from regular expressions to paths to transformers
183+
transform: {
184+
'^.+\\.vue$': '<rootDir>/node_modules/@vue/vue3-jest'
185+
}
186+
187+
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
188+
// transformIgnorePatterns: [
189+
// '/node_modules/',
190+
// '\\.pnp\\.[^\\/]+$'
191+
// ],
192+
193+
// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
194+
// unmockedModulePathPatterns: undefined,
195+
196+
// Indicates whether each individual test should be reported during the run
197+
// verbose: undefined,
198+
199+
// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
200+
// watchPathIgnorePatterns: [],
201+
202+
// Whether to use watchman for file crawling
203+
// watchman: true,
204+
};

0 commit comments

Comments
 (0)