Skip to content

Commit f19cdae

Browse files
authored
Setup Jest (ThatOpen#76)
* ➕ added jest dependency * ✅ first basic test on stringToAxes * 👷 added github action to run tests * 🔧 setup ts-jest to work with ESM modules * ✅ added test on Viewer
1 parent a224dc2 commit f19cdae

File tree

8 files changed

+1341
-55
lines changed

8 files changed

+1341
-55
lines changed

.github/workflows/jest.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name: Jest Tests
2+
on: [pull_request]
3+
jobs:
4+
build:
5+
runs-on: ubuntu-latest
6+
steps:
7+
- uses: actions/checkout@v2
8+
- name: Install modules
9+
run: cd viewer && yarn
10+
- name: Run tests
11+
run: cd viewer && yarn test

viewer/.eslintrc.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ module.exports = {
2424
'no-console': 'off',
2525
'no-param-reassign': 'off',
2626
'eol-last': 'off',
27-
'no-unused-vars': ['error', { argsIgnorePattern: '^_' }],
27+
'no-unused-vars': ['error', {
28+
argsIgnorePattern: '^_'
29+
}],
2830
'class-methods-use-this': 'off',
2931
'import/extensions': [
3032
'error',
@@ -40,8 +42,14 @@ module.exports = {
4042
settings: {
4143
'import/resolver': {
4244
node: {
43-
extensions: ['.js', '.jsx', '.ts', '.tsx', '.d.ts']
44-
}
45-
}
46-
}
47-
};
45+
extensions: ['.js', '.jsx', '.ts', '.tsx', '.d.ts'],
46+
},
47+
},
48+
},
49+
overrides: [{
50+
files: ['*.spec.ts'],
51+
env: {
52+
jest: true,
53+
},
54+
}],
55+
};

viewer/jest.config.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type { InitialOptionsTsJest } from 'ts-jest/dist/types';
2+
3+
const config: InitialOptionsTsJest = {
4+
preset: 'ts-jest',
5+
testEnvironment: 'jsdom',
6+
transform: {
7+
'^.+\\.(t|j)s$': 'ts-jest',
8+
},
9+
globals: {
10+
'ts-jest': {
11+
tsconfig: './tsconfig.jest.json',
12+
},
13+
},
14+
// These node_modules need to be transpiled
15+
// https://stackoverflow.com/a/63390125/3466729
16+
transformIgnorePatterns: ['node_modules/(?!(web-ifc-three|web-ifc|three))'],
17+
};
18+
export default config;

viewer/package.json

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
"main": "dist/index.js",
66
"scripts": {
77
"watch": "tsc -w",
8-
"build": "tsc"
8+
"build": "tsc",
9+
"test": "jest",
10+
"test:watch": "jest --watch"
911
},
1012
"repository": {
1113
"type": "git",
@@ -31,6 +33,7 @@
3133
"web-ifc-three": "^0.0.25"
3234
},
3335
"devDependencies": {
36+
"@types/jest": "^26.0.23",
3437
"@types/node": "^14.14.31",
3538
"@types/three": "^0.127.1",
3639
"@typescript-eslint/eslint-plugin": "^4.27.0",
@@ -40,8 +43,11 @@
4043
"eslint-config-airbnb-base": "^14.2.1",
4144
"eslint-config-prettier": "^8.3.0",
4245
"eslint-plugin-import": "^2.23.4",
46+
"jest": "^27.0.4",
47+
"ts-jest": "^27.0.3",
48+
"ts-node": "^10.0.0",
49+
"typescript": "^4.3.2",
4350
"eslint-plugin-prettier": "^3.4.0",
44-
"prettier": "^2.3.1",
45-
"typescript": "^4.2.4"
51+
"prettier": "^2.3.1"
4652
}
47-
}
53+
}

viewer/src/core/viewer.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Viewer } from './viewer';
2+
3+
jest.mock('three', () => {
4+
const THREE = jest.requireActual('three');
5+
return {
6+
...THREE,
7+
WebGLRenderer: jest.fn().mockReturnValue({
8+
domElement: document.createElement('div'),
9+
setSize: jest.fn(),
10+
render: jest.fn(),
11+
}),
12+
};
13+
});
14+
15+
const THREE = jest.requireActual('three');
16+
17+
describe('Viewer', () => {
18+
it('should create a ThreeJS Scene', () => {
19+
const canvas = document.createElement('canvas');
20+
document.body.appendChild(canvas);
21+
const viewer = new Viewer(canvas);
22+
expect(viewer.scene instanceof THREE.Scene).toBe(true);
23+
});
24+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { stringToAxes } from './ThreeUtils';
2+
3+
describe('ThreeUtils', () => {
4+
describe('stringToAxes', () => {
5+
it('should return null if the parameter does not match the regex', () => {
6+
expect(stringToAxes('abc')).toBeNull();
7+
});
8+
});
9+
});

viewer/tsconfig.jest.json

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
{
2+
"compilerOptions": {
3+
/* Visit https://aka.ms/tsconfig.json to read more about this file */
4+
5+
/* Basic Options */
6+
// "incremental": true, /* Enable incremental compilation */
7+
"target": "ES2018",
8+
/* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
9+
"module": "ESNext",
10+
/* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
11+
// "lib": [], /* Specify library files to be included in the compilation. */
12+
"allowJs": true,
13+
/* Allow javascript files to be compiled. */
14+
// "checkJs": true, /* Report errors in .js files. */
15+
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
16+
"declaration": true,
17+
/* Generates corresponding '.d.ts' file. */
18+
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
19+
"sourceMap": true,
20+
/* Generates corresponding '.map' file. */
21+
// "outFile": "./", /* Concatenate and emit output to single file. */
22+
"outDir": "./dist",
23+
/* Redirect output structure to the directory. */
24+
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
25+
// "composite": true, /* Enable project compilation */
26+
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
27+
// "removeComments": true, /* Do not emit comments to output. */
28+
// "noEmit": true, /* Do not emit outputs. */
29+
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
30+
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
31+
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
32+
33+
/* Strict Type-Checking Options */
34+
"strict": true,
35+
/* Enable all strict type-checking options. */
36+
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
37+
// "strictNullChecks": true, /* Enable strict null checks. */
38+
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
39+
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
40+
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
41+
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
42+
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
43+
44+
/* Additional Checks */
45+
// "noUnusedLocals": true, /* Report errors on unused locals. */
46+
// "noUnusedParameters": true, /* Report errors on unused parameters. */
47+
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
48+
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
49+
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
50+
51+
/* Module Resolution Options */
52+
"moduleResolution": "node",
53+
/* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
54+
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
55+
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
56+
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
57+
// "typeRoots": [], /* List of folders to include type definitions from. */
58+
// "types": [], /* Type declaration files to be included in compilation. */
59+
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
60+
// "esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
61+
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
62+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
63+
64+
/* Source Map Options */
65+
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
66+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
67+
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
68+
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
69+
70+
/* Experimental Options */
71+
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
72+
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
73+
74+
/* Advanced Options */
75+
"skipLibCheck": true,
76+
/* Skip type checking of declaration files. */
77+
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
78+
},
79+
"include": [
80+
"src/**/*"
81+
],
82+
"exclude": ["node_modules"]
83+
}

0 commit comments

Comments
 (0)