-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
basic.test.ts
70 lines (56 loc) · 1.56 KB
/
basic.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { assert, expect, it, suite, test } from 'vitest'
import { two } from '../src/submodule'
import { timeout } from '../src/timeout'
test('Math.sqrt()', async() => {
assert.equal(Math.sqrt(4), two)
assert.equal(Math.sqrt(2), Math.SQRT2)
expect(Math.sqrt(144)).toStrictEqual(12)
// await new Promise(resolve => setTimeout(resolve, 3000))
})
test('JSON', () => {
const input = {
foo: 'hello',
bar: 'world',
}
const output = JSON.stringify(input)
expect(input).toEqual({
foo: 'hello',
bar: 'world',
})
expect(output).toEqual('{"foo":"hello","bar":"world"}')
assert.deepEqual(JSON.parse(output), input, 'matches original')
})
test('assertion is callable', () => {
const str = '13'
expect(str).to.be.a('string')
expect(str).not.to.be.a('number')
})
const hi = suite('suite')
hi.test('expect truthy', () => {
expect({}).toBeTruthy()
expect(null).not.toBeTruthy()
})
// Remove .skip to test async fail by timeout
test.skip('async with timeout', async() => {
return new Promise((resolve) => {
setTimeout(() => {
resolve()
}, 200)
})
}, 100)
it('timeout', () => new Promise(resolve => setTimeout(resolve, timeout)))
function callbackTest(name: string, doneValue: any) {
let callbackAwaited = false
it(`callback setup ${name}`, (done) => {
setTimeout(() => {
expect({}).toBeTruthy()
callbackAwaited = true
done(doneValue)
}, 20)
})
it(`callback test ${name}`, () => {
expect(callbackAwaited).toBe(true)
})
}
callbackTest('success ', undefined)
callbackTest('success done(false)', false)