Skip to content

Commit 22c3923

Browse files
committed
Starts using Jest for tests.
1 parent e6c04bf commit 22c3923

10 files changed

Lines changed: 381 additions & 130 deletions

File tree

tests/.babelrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"presets": ["@babel/preset-env"],
3+
"plugins": ["transform-class-properties"]
4+
}

tests/jest.config.js

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

tests/json/ParserActionTest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,4 +259,4 @@
259259
}
260260
}
261261
}
262-
}
262+
}

tests/package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "0.2.0",
44
"description": "Test framework for MathJax v3 TeX parser.",
55
"scripts": {
6-
"test": "./tests.sh"
6+
"test": "jest"
77
},
88
"maintainers": [
99
"Volker Sorge <V.Sorge@mathjax.org> (http://mathjax.org)"
@@ -14,7 +14,10 @@
1414
},
1515
"license": "Apache-2.0",
1616
"dependencies": {
17-
"mathjax-full": "^3.1.2",
18-
"tape": "^5.0.1"
17+
"@babel/preset-env": "^7.11.5",
18+
"babel-jest": "^26.5.2",
19+
"babel-plugin-transform-class-properties": "^6.24.1",
20+
"jest": "^26.5.2",
21+
"mathjax-full": "^3.1.2"
1922
}
2023
}

tests/src/other-test.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ import ParseUtil from '../node_modules/mathjax-full/js/input/tex/ParseUtil.js';
2929
export class KeyvalTest extends JsonTest {
3030

3131
runTest(name, input, expected) {
32-
this.test(
33-
name,
34-
t => {
35-
try {
36-
let keyval = ParseUtil.keyvalOptions(input);
37-
t.deepEqual(keyval, expected, name);
32+
test(
33+
name,
34+
() => {
35+
try {
36+
let keyval = ParseUtil.keyvalOptions(input);
37+
expect(keyval).toEqual(expected);
3838
} catch (e) {
39-
t.deepEqual(e.message, expected, name);
39+
expect(e.message).toEqual(expected);
4040
}
4141
}
4242
);

tests/src/parser-test.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ export class ParserJsonTest extends JsonTest {
5959
this.name = this.json['name'] || this.name;
6060
this.settings = this.json['settings'] || this.settings;
6161
this.processSettings();
62-
console.log('\u001B\u005B\u0033\u0034\u006D' +
63-
'Running tests from ' + (this.name || this.constructor.name) +
64-
'\u001B\u005B\u0033\u0037\u006D');
62+
// console.log('\u001B\u005B\u0033\u0034\u006D' +
63+
// 'Running tests from ' + (this.name || this.constructor.name) +
64+
// '\u001B\u005B\u0033\u0037\u006D');
6565
}
6666

6767
processSettings() {
@@ -83,20 +83,18 @@ export class ParserJsonTest extends JsonTest {
8383
* @override
8484
*/
8585
runTest(name, tex, expected, rest) {
86-
this.test(
86+
test(
8787
name,
88-
t => {
89-
mathjax.handleRetriesFor(function() {
88+
() => {
89+
return mathjax.handleRetriesFor(function() {
9090
let options = {packages: this.packages};
9191
Object.assign(options, this.settings);
9292
let root = this.document(options).convert(tex, {end: STATE.CONVERT});
9393
let jv = new JsonMmlVisitor();
9494
root.setTeXclass(null);
95-
let actual = jv.visitTree(root);
96-
t.deepEqual(actual, expected, name);
97-
}.bind(this)).catch(err => {
98-
console.log(err.message);
99-
console.log(err.stack.replace(/\n.*\/system\.js:(.|\n)*/, ''));
95+
return jv.visitTree(root);
96+
}.bind(this)).then(data => {
97+
expect(data).toEqual(expected);
10098
});
10199
}
102100
);
@@ -116,6 +114,7 @@ export class ParserOutputTest extends ParserJsonTest {
116114
* @override
117115
*/
118116
document(options) {
117+
console.log('In SVG');
119118
return mathjax.document('<html></html>', {
120119
InputJax: new TeX(options), OutputJax: new SVG()
121120
});

tests/src/parser.test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*************************************************************
2+
*
3+
* Copyright (c) 2020 The MathJax Consortium
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
19+
/**
20+
* @fileoverview The current test runner.
21+
*
22+
* @author v.sorge@mathjax.org (Volker Sorge)
23+
*/
24+
25+
import {TestFactory} from './test-factory.js';
26+
let process = require('process');
27+
28+
29+
let file = null;
30+
31+
if (process.argv.length > 2) {
32+
let last = process.argv[process.argv.length - 1];
33+
if (!last.match(/^--/) && last.match(/\.json/)) {
34+
file = last;
35+
}
36+
}
37+
38+
if (file) {
39+
TestFactory.create(file).runTests();
40+
process.exit;
41+
}
42+
43+
if (!file) {
44+
let tests = TestFactory.allParserTests();
45+
tests.forEach( x => x.runTests());
46+
}
47+

tests/src/test-factory.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export class TestFactory {
5858
if (fs.lstatSync(TestFactory.testDir).isDirectory()) {
5959
files = fs.readdirSync(TestFactory.testDir).filter(x => x.match(/\.json$/));
6060
}
61-
let tests = files.filter(
61+
let tests = files.map(
6262
file => TestFactory.create(TestFactory.testDir + '/' + file));
6363
return tests;
6464
}

0 commit comments

Comments
 (0)