-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: import some jest tests/examples
- Loading branch information
Showing
13 changed files
with
238 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Jest repo tests | ||
|
||
A small subtree of the upstream Jest repo, snapshotted from: | ||
|
||
<https://github.com/jestjs/jest/tree/21cce70205025decc778f4aac2ec76051f589fd7> | ||
|
||
When updating, use the same commit or update the link and incorporate the changes | ||
|
||
## package.json | ||
|
||
Do not change, it's needed to make this subtree operate in CJS mode, which jest uses |
20 changes: 20 additions & 0 deletions
20
__test__/jest/jest-repo/examples/expect-extend/__tests__/ranges.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import {expect, test} from '@jest/globals'; | ||
import '../toBeWithinRange'; | ||
|
||
test('is within range', () => expect(100).toBeWithinRange(90, 110)); | ||
|
||
test('is NOT within range', () => expect(101).not.toBeWithinRange(0, 100)); | ||
|
||
test('asymmetric ranges', () => { | ||
expect({apples: 6, bananas: 3}).toEqual({ | ||
apples: expect.toBeWithinRange(1, 10), | ||
bananas: expect.not.toBeWithinRange(11, 20), | ||
}); | ||
}); |
56 changes: 56 additions & 0 deletions
56
__test__/jest/jest-repo/examples/expect-extend/toBeWithinRange.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import {expect} from '@jest/globals'; | ||
import type {MatcherFunction} from 'expect'; | ||
|
||
const toBeWithinRange: MatcherFunction<[floor: unknown, ceiling: unknown]> = | ||
function (actual, floor, ceiling) { | ||
if ( | ||
typeof actual !== 'number' || | ||
typeof floor !== 'number' || | ||
typeof ceiling !== 'number' | ||
) { | ||
throw new TypeError('These must be of type number!'); | ||
} | ||
|
||
const pass = actual >= floor && actual <= ceiling; | ||
if (pass) { | ||
return { | ||
message: () => | ||
`expected ${this.utils.printReceived( | ||
actual, | ||
)} not to be within range ${this.utils.printExpected( | ||
`${floor} - ${ceiling}`, | ||
)}`, | ||
pass: true, | ||
}; | ||
} else { | ||
return { | ||
message: () => | ||
`expected ${this.utils.printReceived( | ||
actual, | ||
)} to be within range ${this.utils.printExpected( | ||
`${floor} - ${ceiling}`, | ||
)}`, | ||
pass: false, | ||
}; | ||
} | ||
}; | ||
|
||
expect.extend({ | ||
toBeWithinRange, | ||
}); | ||
|
||
declare module 'expect' { | ||
interface AsymmetricMatchers { | ||
toBeWithinRange(floor: number, ceiling: number): void; | ||
} | ||
interface Matchers<R> { | ||
toBeWithinRange(floor: number, ceiling: number): R; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
__test__/jest/jest-repo/examples/manual-mocks/__tests__/user.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Copyright (c) Meta Platforms, Inc. and affiliates.. All Rights Reserved. | ||
|
||
import user from '../models/user'; | ||
|
||
test('if original user model', () => { | ||
expect(user.getAuthenticated()).toEqual({age: 26, name: 'Real name'}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Copyright (c) Meta Platforms, Inc. and affiliates.. All Rights Reserved. | ||
|
||
export default { | ||
getAuthenticated: () => ({ | ||
age: 26, | ||
name: 'Real name', | ||
}), | ||
}; |
30 changes: 30 additions & 0 deletions
30
__test__/jest/jest-repo/examples/timer/__tests__/infinite_timer_game.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright (c) Meta Platforms, Inc. and affiliates.. All Rights Reserved. | ||
|
||
'use strict'; | ||
|
||
jest.useFakeTimers(); | ||
|
||
it('schedules a 10-second timer after 1 second', () => { | ||
jest.spyOn(globalThis, 'setTimeout'); | ||
const infiniteTimerGame = require('../infiniteTimerGame'); | ||
const callback = jest.fn(); | ||
|
||
infiniteTimerGame(callback); | ||
|
||
// At this point in time, there should have been a single call to | ||
// setTimeout to schedule the end of the game in 1 second. | ||
expect(setTimeout).toHaveBeenCalledTimes(1); | ||
expect(setTimeout).toHaveBeenNthCalledWith(1, expect.any(Function), 1000); | ||
|
||
// Fast forward and exhaust only currently pending timers | ||
// (but not any new timers that get created during that process) | ||
jest.runOnlyPendingTimers(); | ||
|
||
// At this point, our 1-second timer should have fired its callback | ||
expect(callback).toHaveBeenCalled(); | ||
|
||
// And it should have created a new timer to start the game over in | ||
// 10 seconds | ||
expect(setTimeout).toHaveBeenCalledTimes(2); | ||
expect(setTimeout).toHaveBeenNthCalledWith(2, expect.any(Function), 10_000); | ||
}); |
52 changes: 52 additions & 0 deletions
52
__test__/jest/jest-repo/examples/timer/__tests__/timer_game.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright (c) Meta Platforms, Inc. and affiliates.. All Rights Reserved. | ||
|
||
'use strict'; | ||
|
||
jest.useFakeTimers(); | ||
|
||
describe('timerGame', () => { | ||
beforeEach(() => { | ||
jest.spyOn(globalThis, 'setTimeout'); | ||
}); | ||
it('waits 1 second before ending the game', () => { | ||
const timerGame = require('../timerGame'); | ||
timerGame(); | ||
|
||
expect(setTimeout).toHaveBeenCalledTimes(1); | ||
expect(setTimeout).toHaveBeenCalledWith(expect.any(Function), 1000); | ||
}); | ||
|
||
it('calls the callback after 1 second via runAllTimers', () => { | ||
const timerGame = require('../timerGame'); | ||
const callback = jest.fn(); | ||
|
||
timerGame(callback); | ||
|
||
// At this point in time, the callback should not have been called yet | ||
expect(callback).not.toHaveBeenCalled(); | ||
|
||
// Fast-forward until all timers have been executed | ||
jest.runAllTimers(); | ||
|
||
// Now our callback should have been called! | ||
expect(callback).toHaveBeenCalled(); | ||
expect(callback).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('calls the callback after 1 second via advanceTimersByTime', () => { | ||
const timerGame = require('../timerGame'); | ||
const callback = jest.fn(); | ||
|
||
timerGame(callback); | ||
|
||
// At this point in time, the callback should not have been called yet | ||
expect(callback).not.toHaveBeenCalled(); | ||
|
||
// Fast-forward until all timers have been executed | ||
jest.advanceTimersByTime(1000); | ||
|
||
// Now our callback should have been called! | ||
expect(callback).toHaveBeenCalled(); | ||
expect(callback).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
17 changes: 17 additions & 0 deletions
17
__test__/jest/jest-repo/examples/timer/infiniteTimerGame.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright (c) Meta Platforms, Inc. and affiliates.. All Rights Reserved. | ||
|
||
function infiniteTimerGame(callback) { | ||
console.log('Ready....go!'); | ||
|
||
setTimeout(() => { | ||
console.log('Times up! 10 seconds before the next game starts...'); | ||
callback && callback(); | ||
|
||
// Schedule the next game in 10 seconds | ||
setTimeout(() => { | ||
infiniteTimerGame(callback); | ||
}, 10_000); | ||
}, 1000); | ||
} | ||
|
||
module.exports = infiniteTimerGame; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright (c) Meta Platforms, Inc. and affiliates.. All Rights Reserved. | ||
|
||
function timerGame(callback) { | ||
console.log('Ready....go!'); | ||
setTimeout(() => { | ||
console.log('Times up -- stop!'); | ||
callback && callback(); | ||
}, 1000); | ||
} | ||
|
||
module.exports = timerGame; |
13 changes: 13 additions & 0 deletions
13
__test__/jest/jest-repo/examples/typescript/__tests__/sum.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright (c) Meta Platforms, Inc. and affiliates.. All Rights Reserved. | ||
|
||
import {expect, it} from '@jest/globals'; | ||
|
||
it('adds 1 + 2 to equal 3 in Typescript', () => { | ||
const sum = require('../sum.ts').default; | ||
expect(sum(1, 2)).toBe(3); | ||
}); | ||
|
||
it('adds 1 + 2 to equal 3 in JavaScript', () => { | ||
const sum = require('../sum.js'); | ||
expect(sum(1, 2)).toBe(3); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Copyright (c) Meta Platforms, Inc. and affiliates.. All Rights Reserved. | ||
|
||
function sum(a, b) { | ||
return a + b; | ||
} | ||
|
||
module.exports = sum; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Copyright (c) Meta Platforms, Inc. and affiliates. | ||
|
||
const sum = (a: number, b: number): number => a + b; | ||
|
||
export default sum; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{ "type": "commonjs" } |