Skip to content

Commit 6db46aa

Browse files
committed
test: (wip) run tests in browser to test compat
1 parent 285e3b4 commit 6db46aa

File tree

8 files changed

+5951
-0
lines changed

8 files changed

+5951
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function sum(a, b) {
2+
return a + b;
3+
}
4+
5+
test("adds 1 + 2 to equal 3", () => {
6+
expect(sum(1, 2)).toBe(3);
7+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import type { Config } from "@jest/types";
2+
import { pathsToModuleNameMapper } from "ts-jest/utils";
3+
import { compilerOptions } from "../../tsconfig.json";
4+
5+
const config: Config.InitialOptions = {
6+
preset: "ts-jest",
7+
testRegex: "(\\/__tests__\\/.*|\\.(test|spec))\\.(ts)$",
8+
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, {
9+
// This has to match the baseUrl defined in tsconfig.json.
10+
prefix: "<rootDir>/../../",
11+
}),
12+
modulePathIgnorePatterns: ["__tests__/*.d.ts"],
13+
collectCoverage: true,
14+
collectCoverageFrom: ["<rootDir>/src/**/*.ts", "!**/node_modules/**"],
15+
};
16+
17+
export default config;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// the jest.fn() API
2+
import jest from "jest-mock";
3+
// // The matchers API
4+
import expect from "expect";
5+
6+
// Add missing Jest functions
7+
window.test = window.it;
8+
window.test.each = (inputs) => {
9+
return (testName, test) => {
10+
return inputs.forEach((args) => {
11+
window.it(testName, () => test(...args));
12+
});
13+
};
14+
};
15+
window.test.todo = function () {
16+
return undefined;
17+
};
18+
window.jest = jest;
19+
window.expect = expect;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
module.exports = function (config) {
2+
config.set({
3+
plugins: ["karma-webpack", "karma-jasmine", "karma-chrome-launcher"],
4+
5+
// base path that will be used to resolve all patterns (eg. files, exclude)
6+
basePath: "",
7+
8+
// frameworks to use
9+
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
10+
frameworks: ["jasmine"],
11+
12+
// list of files / patterns to load in the browser
13+
// Here I'm including all of the the Jest tests which are all under the __tests__ directory.
14+
// You may need to tweak this patter to find your test files/
15+
files: ["./karma-setup.js", "__tests__/**/*.ts"],
16+
17+
// preprocess matching files before serving them to the browser
18+
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
19+
preprocessors: {
20+
"./karma-setup.js": ["webpack"],
21+
// Use webpack to bundle our tests files
22+
"__tests__/**/*.ts": ["webpack"],
23+
},
24+
25+
browsers: ["ChromeHeadless"],
26+
27+
singleRun: true,
28+
29+
webpack: {
30+
watch: false,
31+
module: {
32+
rules: [
33+
{
34+
test: /\.ts?$/,
35+
use: "ts-loader",
36+
exclude: /node_modules/,
37+
},
38+
],
39+
},
40+
resolve: {
41+
alias: {
42+
fs: false,
43+
process: false,
44+
},
45+
extensions: [".ts", ".js"],
46+
fallback: {
47+
// fs: false,
48+
buffer: require.resolve("buffer/"),
49+
util: require.resolve("util/"),
50+
assert: require.resolve("assert/"),
51+
stream: require.resolve("stream-browserify/"),
52+
constants: require.resolve("constants-browserify/"),
53+
path: require.resolve("path-browserify/"),
54+
},
55+
},
56+
},
57+
});
58+
};

0 commit comments

Comments
 (0)