Skip to content

Commit b027cb7

Browse files
egor-romanovlaktek
authored andcommitted
feat: move to jest
1 parent 8f72fdc commit b027cb7

File tree

9 files changed

+161
-117
lines changed

9 files changed

+161
-117
lines changed

.mocharc.yml

Lines changed: 0 additions & 21 deletions
This file was deleted.

config.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

jest.config.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type { Config } from '@jest/types';
2+
3+
const config: Config.InitialOptions = {
4+
preset: 'ts-jest',
5+
transform: {
6+
'^.+\\.ts?$': 'ts-jest',
7+
},
8+
moduleFileExtensions: ['ts', 'js', 'cjs', 'json', 'node',],
9+
setupFilesAfterEnv: [
10+
'./test/utils/jest-custom-reporter.ts',
11+
],
12+
testEnvironment: 'jest-circus-allure-environment',
13+
testTimeout: 60000,
14+
coverageThreshold: {
15+
global: {
16+
branches: 0,
17+
functions: 0,
18+
lines: 0,
19+
statements: 0,
20+
},
21+
},
22+
};
23+
export default config;

package.json

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"docs": "typedoc src/index.ts src/types.ts --excludePrivate --excludeProtected",
1616
"docs:json": "typedoc --json docs/spec.json --excludeExternals src/index.ts src/types.ts",
1717
"test": "mocha",
18+
"test": "jest",
1819
"allure:generate": "rm -rf allure-report && node_modules/allure-commandline/bin/allure generate",
1920
"allure:serve": "node_modules/allure-commandline/bin/allure serve",
2021
"test:report": "npm run allure:generate && npm run allure:serve"
@@ -41,31 +42,26 @@
4142
"cross-fetch": "^3.1.5"
4243
},
4344
"devDependencies": {
44-
"@types/chai": "^4.3.0",
45+
"@types/jest": "^27.4.1",
4546
"@types/jsonwebtoken": "^8.5.8",
46-
"@types/mocha": "^9.1.0",
4747
"@types/node": "^17.0.23",
4848
"allure-commandline": "^2.17.2",
49-
"chai": "^4.3.6",
5049
"genversion": "^3.0.2",
50+
"jest": "^27.5.1",
51+
"jest-circus-allure-environment": "^1.1.1",
5152
"jsonwebtoken": "^8.5.1",
52-
"mocha": "^8.4.0",
53-
"mocha-allure2-reporter": "^0.0.3",
54-
"mocha-multi-reporters": "^1.5.1",
5553
"nanoid": "^3.3.1",
5654
"npm-run-all": "^4.1.5",
5755
"prettier": "^2.6.0",
5856
"rimraf": "^3.0.2",
5957
"semantic-release": "^19.0.2",
6058
"semantic-release-plugin-update-version-in-files": "^1.1.0",
61-
<<<<<<< HEAD
6259
"typedoc": "^0.22.16",
63-
=======
6460
"testcontainers": "^8.5.1",
61+
"ts-jest": "^27.1.4",
6562
"ts-node": "^10.7.0",
6663
"ts-test-decorators": "^0.0.6",
6764
"typedoc": "^0.22.13",
68-
>>>>>>> b3a0615 (feat: add basic test and testing core)
6965
"typescript": "^4.6.2"
7066
},
7167
"publishConfig": {

test/relay/container.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ import crossFetch from 'cross-fetch'
44
import { sign } from 'jsonwebtoken'
55
import { GenericContainer, Network, StartedTestContainer, Wait } from 'testcontainers'
66
import { ExecResult } from 'testcontainers/dist/docker/types'
7-
import { ContentType } from 'allure2-js-commons'
8-
9-
import { attach, log } from '../utils/allure'
7+
import { attach, log } from '../utils/jest-custom-reporter'
8+
import { ContentType } from 'allure-js-commons'
109

1110
/**
1211
* A Relay contains a running relay container that has a unique ID and two promises:

test/spec/hello.spec.ts

Lines changed: 81 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
1-
import 'mocha'
2-
import { assert } from 'chai'
1+
import 'jest'
32
import { nanoid } from 'nanoid'
43
import { sign } from 'jsonwebtoken'
5-
import { ContentType } from 'allure2-js-commons'
4+
import { ContentType } from 'allure-js-commons'
65

76
import { FunctionsClient } from '../../src/index'
87

98
import { Relay, runRelay } from '../relay/container'
10-
import { attach, log } from '../utils/allure'
9+
import { attach, log } from '../utils/jest-custom-reporter'
1110
import { getCustomFetch } from '../utils/fetch'
1211

1312
describe('basic tests (hello function)', () => {
1413
let relay: Relay
1514
const jwtSecret = nanoid(10)
1615
const apiKey = sign({ name: 'anon' }, jwtSecret)
1716

18-
before(async () => {
17+
beforeAll(async () => {
1918
relay = await runRelay('hello', jwtSecret)
2019
})
2120

22-
after(async () => {
21+
afterAll(async () => {
2322
relay && relay.container && (await relay.container.stop())
2423
})
2524

26-
it('invoke hello with auth header', async () => {
25+
test('invoke hello with auth header', async () => {
26+
/**
27+
* @feature auth
28+
*/
2729
log('create FunctionsClient')
2830
const fclient = new FunctionsClient(`http://localhost:${relay.container.getMappedPort(8081)}`, {
2931
Authorization: `Bearer ${apiKey}`,
@@ -33,12 +35,15 @@ describe('basic tests (hello function)', () => {
3335
const { data, error } = await fclient.invoke<string>('hello', { responseType: 'text' })
3436

3537
log('assert no error')
36-
assert.isNull(error)
38+
expect(error).toBeNull()
3739
log(`assert ${data} is equal to 'Hello World'`)
38-
assert.equal(data, 'Hello World')
40+
expect(data).toEqual('Hello World')
3941
})
4042

41-
it('invoke hello with setAuth', async () => {
43+
test('invoke hello with setAuth', async () => {
44+
/**
45+
* @feature auth
46+
*/
4247
log('create FunctionsClient')
4348
const fclient = new FunctionsClient(`http://localhost:${relay.container.getMappedPort(8081)}`)
4449
attach('setAuth', apiKey, ContentType.TEXT)
@@ -48,12 +53,15 @@ describe('basic tests (hello function)', () => {
4853
const { data, error } = await fclient.invoke<string>('hello', { responseType: 'text' })
4954

5055
log('assert no error')
51-
assert.isNull(error)
56+
expect(error).toBeNull()
5257
log(`assert ${data} is equal to 'Hello World'`)
53-
assert.equal(data, 'Hello World')
58+
expect(data).toEqual('Hello World')
5459
})
5560

56-
it('invoke hello with setAuth wrong key', async () => {
61+
test('invoke hello with setAuth wrong key', async () => {
62+
/**
63+
* @feature errors
64+
*/
5765
log('create FunctionsClient')
5866
const fclient = new FunctionsClient(`http://localhost:${relay.container.getMappedPort(8081)}`)
5967
const wrongKey = sign({ name: 'anon' }, 'wrong_jwt')
@@ -64,12 +72,15 @@ describe('basic tests (hello function)', () => {
6472
const { data, error } = await fclient.invoke<string>('hello', { responseType: 'text' })
6573

6674
log('check error')
67-
assert.isNotNull(error)
68-
assert.equal(error?.message, 'Invalid JWT')
69-
assert.isNull(data)
75+
expect(error).not.toBeNull()
76+
expect(error?.message).toEqual('Invalid JWT')
77+
expect(data).toBeNull()
7078
})
7179

72-
it('invoke hello: auth override by setAuth wrong key', async () => {
80+
test('invoke hello: auth override by setAuth wrong key', async () => {
81+
/**
82+
* @feature auth
83+
*/
7384
log('create FunctionsClient')
7485
const fclient = new FunctionsClient(`http://localhost:${relay.container.getMappedPort(8081)}`, {
7586
Authorization: `Bearer ${apiKey}`,
@@ -82,12 +93,15 @@ describe('basic tests (hello function)', () => {
8293
const { data, error } = await fclient.invoke<string>('hello', { responseType: 'text' })
8394

8495
log('check error')
85-
assert.isNotNull(error)
86-
assert.equal(error?.message, 'Invalid JWT')
87-
assert.isNull(data)
96+
expect(error).not.toBeNull()
97+
expect(error?.message).toEqual('Invalid JWT')
98+
expect(data).toBeNull()
8899
})
89100

90-
it('invoke hello: auth override by setAuth right key', async () => {
101+
test('invoke hello: auth override by setAuth right key', async () => {
102+
/**
103+
* @feature auth
104+
*/
91105
const wrongKey = sign({ name: 'anon' }, 'wrong_jwt')
92106

93107
log('create FunctionsClient with wrong jwt')
@@ -102,12 +116,15 @@ describe('basic tests (hello function)', () => {
102116
const { data, error } = await fclient.invoke<string>('hello', { responseType: 'text' })
103117

104118
log('assert no error')
105-
assert.isNull(error)
119+
expect(error).toBeNull()
106120
log(`assert ${data} is equal to 'Hello World'`)
107-
assert.equal(data, 'Hello World')
121+
expect(data).toEqual('Hello World')
108122
})
109123

110-
it('invoke hello with auth header in invoke', async () => {
124+
test('invoke hello with auth header in invoke', async () => {
125+
/**
126+
* @feature auth
127+
*/
111128
log('create FunctionsClient')
112129
const fclient = new FunctionsClient(`http://localhost:${relay.container.getMappedPort(8081)}`)
113130

@@ -120,12 +137,15 @@ describe('basic tests (hello function)', () => {
120137
})
121138

122139
log('assert no error')
123-
assert.isNull(error)
140+
expect(error).toBeNull()
124141
log(`assert ${data} is equal to 'Hello World'`)
125-
assert.equal(data, 'Hello World')
142+
expect(data).toEqual('Hello World')
126143
})
127144

128-
it('invoke hello with auth header override in invoke', async () => {
145+
test('invoke hello with auth header override in invoke', async () => {
146+
/**
147+
* @feature auth
148+
*/
129149
log('create FunctionsClient with wrong jwt')
130150
const fclient = new FunctionsClient(`http://localhost:${relay.container.getMappedPort(8081)}`)
131151

@@ -142,12 +162,15 @@ describe('basic tests (hello function)', () => {
142162
})
143163

144164
log('assert no error')
145-
assert.isNull(error)
165+
expect(error).toBeNull()
146166
log(`assert ${data} is equal to 'Hello World'`)
147-
assert.equal(data, 'Hello World')
167+
expect(data).toEqual('Hello World')
148168
})
149169

150-
it('invoke hello with wrong auth header overridden in invoke', async () => {
170+
test('invoke hello with wrong auth header overridden in invoke', async () => {
171+
/**
172+
* @feature auth
173+
*/
151174
log('create FunctionsClient with wrong jwt')
152175
const fclient = new FunctionsClient(`http://localhost:${relay.container.getMappedPort(8081)}`, {
153176
Authorization: `Bearer ${apiKey}`,
@@ -163,12 +186,15 @@ describe('basic tests (hello function)', () => {
163186
})
164187

165188
log('check error')
166-
assert.isNotNull(error)
167-
assert.equal(error?.message, 'Invalid JWT')
168-
assert.isNull(data)
189+
expect(error).not.toBeNull()
190+
expect(error?.message).toEqual('Invalid JWT')
191+
expect(data).toBeNull()
169192
})
170193

171194
it.skip('invoke missing function', async () => {
195+
/**
196+
* @feature errors
197+
*/
172198
log('create FunctionsClient')
173199
const fclient = new FunctionsClient(`http://localhost:${relay.container.getMappedPort(8081)}`, {
174200
Authorization: `Bearer ${apiKey}`,
@@ -178,12 +204,15 @@ describe('basic tests (hello function)', () => {
178204
const { data, error } = await fclient.invoke<string>('missing', { responseType: 'text' })
179205

180206
log('check error')
181-
assert.isNotNull(error)
182-
assert.equal(error?.message, 'Invalid JWT')
183-
assert.isNull(data)
207+
expect(error).not.toBeNull()
208+
expect(error?.message).toEqual('Invalid JWT')
209+
expect(data).toBeNull()
184210
})
185211

186-
it('invoke with custom fetch', async () => {
212+
test('invoke with custom fetch', async () => {
213+
/**
214+
* @feature fetch
215+
*/
187216
log('create FunctionsClient')
188217
const fclient = new FunctionsClient(
189218
`http://localhost:${relay.container.getMappedPort(8081)}`,
@@ -200,12 +229,15 @@ describe('basic tests (hello function)', () => {
200229
const { data, error } = await fclient.invoke<string>('', { responseType: 'text' })
201230

202231
log('assert no error')
203-
assert.isNull(error)
232+
expect(error).toBeNull()
204233
log(`assert ${data} is equal to 'Hello World'`)
205-
assert.equal(data, 'Hello World')
234+
expect(data).toEqual('Hello World')
206235
})
207236

208-
it('invoke with custom fetch wrong method', async () => {
237+
test('invoke with custom fetch wrong method', async () => {
238+
/**
239+
* @feature fetch
240+
*/
209241
log('create FunctionsClient')
210242
const fclient = new FunctionsClient(
211243
`http://localhost:${relay.container.getMappedPort(8081)}`,
@@ -222,13 +254,16 @@ describe('basic tests (hello function)', () => {
222254
const { data, error } = await fclient.invoke<string>('', { responseType: 'text' })
223255

224256
log('check error')
225-
assert.isNotNull(error)
257+
expect(error).not.toBeNull()
226258
log(`assert ${error?.message} is equal to 'Only POST and OPTIONS requests are supported'`)
227-
assert.equal(error?.message, 'Only POST and OPTIONS requests are supported')
228-
assert.isNull(data)
259+
expect(error?.message).toEqual('Only POST and OPTIONS requests are supported')
260+
expect(data).toBeNull()
229261
})
230262

231-
it('invoke hello with custom fetch override header', async () => {
263+
test('invoke hello with custom fetch override header', async () => {
264+
/**
265+
* @feature fetch
266+
*/
232267
const wrongKey = sign({ name: 'anon' }, 'wrong_jwt')
233268
log('create FunctionsClient')
234269
const fclient = new FunctionsClient(
@@ -250,8 +285,8 @@ describe('basic tests (hello function)', () => {
250285
})
251286

252287
log('assert no error')
253-
assert.isNull(error)
288+
expect(error).toBeNull()
254289
log(`assert ${data} is equal to 'Hello World'`)
255-
assert.equal(data, 'Hello World')
290+
expect(data).toEqual('Hello World')
256291
})
257292
})

0 commit comments

Comments
 (0)