Skip to content

Commit 4529b54

Browse files
committed
Add setup files for jest and karma
1 parent 74de850 commit 4529b54

10 files changed

Lines changed: 214 additions & 0 deletions

File tree

test/_setupFiles/babel.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require('@babel/register')({
2+
'extensions': ['.js', '.ts']
3+
});

test/_setupFiles/bootstrap.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* This script file presents you the opportunity of running some code immediately
3+
* after the test framework has been installed in the environment.
4+
*/
5+
import {HyperFormula} from '../../src'
6+
import {Config} from '../../src/Config'
7+
import {enGB} from '../../src/i18n/languages'
8+
import * as plugins from '../../src/interpreter/plugin'
9+
import {toContainEqualMatcher, toEqualErrorMatcher, toMatchObjectMatcher} from './matchers'
10+
11+
function unregisterAllLanguages() {
12+
for (const langCode of HyperFormula.getRegisteredLanguagesCodes()) {
13+
HyperFormula.unregisterLanguage(langCode)
14+
}
15+
}
16+
17+
Config.defaultConfig = Object.assign({}, Config.defaultConfig, {
18+
functionPlugins: [],
19+
useStats: true,
20+
licenseKey: 'gpl-v3',
21+
})
22+
23+
const jestPresent = (() => {
24+
try {
25+
expect([{a: 0}]).toContainEqual({a: 0})
26+
return true
27+
} catch (e) {
28+
return false
29+
}
30+
})()
31+
32+
beforeEach(() => {
33+
if (!jestPresent) {
34+
(jasmine as any).setDefaultSpyStrategy((and: any) => and.callThrough())
35+
}
36+
37+
unregisterAllLanguages()
38+
39+
const defaultLanguage = Config.defaultConfig.language
40+
41+
HyperFormula.registerLanguage(defaultLanguage, enGB)
42+
43+
HyperFormula.unregisterAllFunctions()
44+
45+
for (const pluginName of Object.getOwnPropertyNames(plugins)) {
46+
if (!pluginName.startsWith('_')) {
47+
HyperFormula.registerFunctionPlugin(plugins[pluginName as keyof typeof plugins])
48+
}
49+
}
50+
})
51+
52+
beforeAll(() => {
53+
if (!jestPresent) {
54+
(jasmine as any).addMatchers({
55+
...toContainEqualMatcher,
56+
...toMatchObjectMatcher,
57+
...toEqualErrorMatcher,
58+
})
59+
} else {
60+
if (global.gc) {
61+
global.gc()
62+
}
63+
}
64+
})

test/_setupFiles/globalSetup.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* This script file presents you the opportunity of running some code immediately
3+
* before the test framework has been installed in the environment.
4+
*/
5+
// eslint-disable-next-line @typescript-eslint/no-var-requires
6+
const htConfig = require('./../../ht.config')
7+
8+
export default function() {
9+
// Extract all HF constants to the process environment namespance.
10+
// So the HT_RELEASE_DATE consts and other will be accessible while
11+
// testing the lib.
12+
Object.keys(htConfig).forEach((key) => {
13+
process.env[key] = htConfig[key]
14+
})
15+
}

test/_setupFiles/jest/bootstrap.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import {toEqualError} from './toEqualError'
2+
3+
beforeAll(() => {
4+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
5+
// @ts-ignore
6+
// eslint-disable-next-line no-global-assign
7+
spyOn = jest.spyOn
8+
expect.extend(toEqualError)
9+
})
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
type CustomMatcherResult = jest.CustomMatcherResult
2+
type ExpectExtendMap = jest.ExpectExtendMap
3+
4+
declare global {
5+
namespace jest {
6+
interface Matchers<R, T> {
7+
toEqualError(expected: any): CustomMatcherResult,
8+
}
9+
}
10+
}
11+
12+
export const toEqualError: ExpectExtendMap = {
13+
toEqualError(received: any, expected: any): CustomMatcherResult {
14+
let result = false
15+
16+
if (typeof received === 'object' && typeof expected === 'object' && received.message != null && expected.message != null && received.message.includes(expected.message)) {
17+
result = this.equals(
18+
{...received, message: undefined, root: undefined, address: undefined},
19+
{...expected, message: undefined, root: undefined, address: undefined}
20+
)
21+
} else {
22+
result = this.equals(received, expected)
23+
}
24+
return {
25+
pass: result,
26+
message: () => (result ? '' : `Expected ${JSON.stringify(received, null, 2)} to match ${JSON.stringify(expected, null, 2)}.`)
27+
}
28+
}
29+
}

test/_setupFiles/jsdom.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {JSDOM} from 'jsdom'
2+
3+
const dom = new JSDOM('<html><body></body></html>');
4+
5+
global.document = dom.window.document;
6+
global.window = dom.window;
7+
global.navigator = dom.window.navigator;

test/_setupFiles/matchers/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export {toContainEqualMatcher} from './toContainEqual'
2+
export {toMatchObjectMatcher} from './toMatchObject'
3+
export {toEqualErrorMatcher} from './toEqualError'
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
type CustomMatcher = jasmine.CustomMatcher
2+
type CustomMatcherFactories = jasmine.CustomMatcherFactories
3+
type CustomMatcherResult = jasmine.CustomMatcherResult
4+
type MatchersUtil = jasmine.MatchersUtil
5+
6+
declare global {
7+
namespace jasmine {
8+
interface Matchers<T> {
9+
toContainEqual(expected: object, expectationFailOutput?: string): boolean,
10+
}
11+
}
12+
}
13+
14+
export const toContainEqualMatcher: CustomMatcherFactories = {
15+
toContainEqual: function(util: MatchersUtil): CustomMatcher {
16+
return {
17+
compare: function(actual: any[], expected: any): CustomMatcherResult {
18+
return {
19+
pass: util.equals(actual, jasmine.arrayContaining([expected])),
20+
message: ''
21+
}
22+
},
23+
}
24+
}
25+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
type CustomMatcher = jasmine.CustomMatcher
2+
type CustomMatcherFactories = jasmine.CustomMatcherFactories
3+
type CustomMatcherResult = jasmine.CustomMatcherResult
4+
type MatchersUtil = jasmine.MatchersUtil
5+
6+
declare global {
7+
namespace jasmine {
8+
interface Matchers<T> {
9+
toEqualError(expected: any, expectationFailOutput?: string): boolean,
10+
}
11+
}
12+
}
13+
14+
export const toEqualErrorMatcher: CustomMatcherFactories = {
15+
toEqualError: function(util: MatchersUtil): CustomMatcher {
16+
return {
17+
compare: function(received: any, expected: any): CustomMatcherResult {
18+
let result
19+
if (typeof received === 'object' && typeof expected === 'object' && received.message != null && expected.message != null && received.message.includes(expected.message)) {
20+
result = util.equals(
21+
{...received, message: undefined, root: undefined, address: undefined},
22+
{...expected, message: undefined, root: undefined, address: undefined}
23+
)
24+
} else {
25+
result = util.equals(received, expected)
26+
}
27+
return {
28+
pass: result,
29+
message: result ? '' : `Expected ${JSON.stringify(received, null, 2)} to match ${JSON.stringify(expected, null, 2)}.`
30+
}
31+
},
32+
}
33+
}
34+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
type CustomMatcher = jasmine.CustomMatcher
2+
type CustomMatcherFactories = jasmine.CustomMatcherFactories
3+
type CustomMatcherResult = jasmine.CustomMatcherResult
4+
type MatchersUtil = jasmine.MatchersUtil
5+
6+
declare global {
7+
namespace jasmine {
8+
interface Matchers<T> {
9+
toMatchObject(expected: object, expectationFailOutput?: string): boolean,
10+
}
11+
}
12+
}
13+
14+
export const toMatchObjectMatcher: CustomMatcherFactories = {
15+
toMatchObject: function(util: MatchersUtil): CustomMatcher {
16+
return {
17+
compare: function(actual: never, expected: never): CustomMatcherResult {
18+
return {
19+
pass: util.equals(actual, jasmine.objectContaining(expected)),
20+
message: ''
21+
}
22+
},
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)