Clean up jest (#50177)
This commit is contained in:
@@ -5,10 +5,9 @@ always open a pull request and rely on the CI service to run tests for you,
|
||||
but it's helpful to run tests locally before pushing your changes to
|
||||
GitHub.
|
||||
|
||||
Tests are written using [jest](https://ghub.io/jest), a framework maintained
|
||||
by Facebook and used by many teams at GitHub.
|
||||
Jest provides everything: a test runner, an assertion library, code coverage analysis,
|
||||
custom reporters for different types of test output, etc.
|
||||
Tests are written using [vitest](https://vitest.dev/).
|
||||
|
||||
`vitest` runs tests and handles assertions.
|
||||
|
||||
### Install optional dependencies
|
||||
|
||||
@@ -48,7 +47,7 @@ You can run specific tests in two ways:
|
||||
# The TEST_NAME can be a filename, partial filename, or path to a file or directory
|
||||
npm test -- <TEST_NAME>
|
||||
|
||||
NODE_OPTIONS=--experimental-vm-modules npx jest tests/unit
|
||||
vitest path/to/tests/directory
|
||||
```
|
||||
|
||||
### Failed Local Tests
|
||||
@@ -74,8 +73,8 @@ npm run lint
|
||||
|
||||
### Keeping the server running
|
||||
|
||||
When you run `jest` tests that depend on making real HTTP requests
|
||||
to `localhost:4000`, the `jest` tests have a hook that starts the
|
||||
When you run `vitest` tests that depend on making real HTTP requests
|
||||
to `localhost:4000`, the `vitest` tests have a hook that starts the
|
||||
server before running all/any tests and stops the server when done.
|
||||
|
||||
You can disable this, which might make it easier when debugging tests
|
||||
@@ -90,13 +89,13 @@ NODE_ENV=test PORT=4000 tsx src/frame/server.ts
|
||||
In another terminal, type:
|
||||
|
||||
```shell
|
||||
START_JEST_SERVER=false jest tests/rendering/foo/bar.js
|
||||
START_VITEST_SERVER=false vitests src/versions/tests
|
||||
```
|
||||
|
||||
Or whatever the testing command you use is.
|
||||
|
||||
The `START_JEST_SERVER` environment variable needs to be set to `false`, or else `jest` will try to start
|
||||
a server on `:4000` too.
|
||||
The `START_VITEST_SERVER` environment variable needs to be set to `false`,
|
||||
or else `vitest` will try to start a server on `:4000` too.
|
||||
|
||||
### Debugging middleware errors
|
||||
|
||||
@@ -109,7 +108,7 @@ error is happening, set `$DEBUG_MIDDLEWARE_TESTS` to `true`. For example:
|
||||
|
||||
```shell
|
||||
export DEBUG_MIDDLEWARE_TESTS=true
|
||||
jest tests/rendering/ -b
|
||||
vitest src/shielding/tests -b
|
||||
```
|
||||
|
||||
### Fixture based testing
|
||||
|
||||
@@ -1,50 +0,0 @@
|
||||
import { jest } from '@jest/globals'
|
||||
|
||||
export function coreMock() {
|
||||
return {
|
||||
info: jest.fn(),
|
||||
warn: jest.fn(),
|
||||
error: jest.fn(console.error),
|
||||
setOutput: jest.fn(),
|
||||
}
|
||||
}
|
||||
|
||||
export function octokitMock({ requestMock, listForRepoMock } = {}) {
|
||||
return {
|
||||
request: jest.fn(requestMock),
|
||||
rest: {
|
||||
issues: {
|
||||
listForRepo: jest.fn(listForRepoMock),
|
||||
createComment: jest.fn(),
|
||||
update: jest.fn(),
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
export function cheerioMock(argToValueMap) {
|
||||
return {
|
||||
load: jest.fn(async () => {
|
||||
return (arg) => {
|
||||
return argToValueMap[arg]
|
||||
}
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
export function gotMock({ status } = {}) {
|
||||
return jest.fn(async () => {
|
||||
if (status < 200 || status >= 400) {
|
||||
throw new Error({
|
||||
status,
|
||||
})
|
||||
}
|
||||
return new Error({
|
||||
status,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export function uploadArtifactMock() {
|
||||
return jest.fn()
|
||||
}
|
||||
@@ -25,7 +25,7 @@ function stripLiquid(text) {
|
||||
// Given a URI that does not start with a specific language,
|
||||
// return undefined if it can found as a known page.
|
||||
// Otherwise, return an object with information that is used to
|
||||
// print a useful jest error message in the assertion.
|
||||
// print a useful test error message in the assertion.
|
||||
export function checkURL(uri, index, redirectsContext) {
|
||||
const url = `/en${stripLiquid(uri).split('#')[0]}`
|
||||
if (!(url in redirectsContext.pages)) {
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
import failOnConsole from 'jest-fail-on-console'
|
||||
|
||||
// Jest tests don't fail in some cases where we would see an error in DevTools
|
||||
// Console when running locally and we would also see the error in the test
|
||||
// output. This includes the React `Each child in a list should have a unique
|
||||
// "key" prop` error example.
|
||||
//
|
||||
// To catch this and fail tests in cases like this, we use `jest-fail-on-console`
|
||||
// to fail on calls to `console.error()`.
|
||||
failOnConsole({
|
||||
shouldFailOnWarn: false,
|
||||
})
|
||||
@@ -1,13 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { START_JEST_SERVER, isServerHealthy, killServer } from './server-for-jest.js'
|
||||
|
||||
export default async () => {
|
||||
if (START_JEST_SERVER) {
|
||||
global.__SERVER__.close()
|
||||
|
||||
if (await isServerHealthy()) {
|
||||
killServer()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
import kill from 'kill-port'
|
||||
import portUsed from 'port-used'
|
||||
import got, { RequestError } from 'got'
|
||||
|
||||
export const PORT = 4000
|
||||
|
||||
// By default it's on
|
||||
export const START_JEST_SERVER = Boolean(JSON.parse(process.env.START_JEST_SERVER || 1))
|
||||
|
||||
export async function isServerHealthy() {
|
||||
try {
|
||||
const res = await got.head(`http://localhost:${PORT}/healthz`, { retry: { limit: 0 } })
|
||||
return res.statusCode === 200
|
||||
} catch (err) {
|
||||
// This exception is thrown if you can't even connect.
|
||||
if (err instanceof RequestError) {
|
||||
return false
|
||||
}
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
export function killServer() {
|
||||
kill(PORT, 'tcp')
|
||||
.then(() => {
|
||||
console.log(`Killed what was on :${PORT}`)
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log(`Unable to kill whatever was on :${PORT}:`, error)
|
||||
})
|
||||
}
|
||||
|
||||
export async function isPortRunning() {
|
||||
return await portUsed.check(PORT)
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { main } from '#src/frame/start-server.js'
|
||||
|
||||
import { PORT, START_JEST_SERVER, isServerHealthy, isPortRunning } from './server-for-jest.js'
|
||||
|
||||
export default async () => {
|
||||
if (START_JEST_SERVER) {
|
||||
console.log(`Starting a server for jest on port :${PORT}.`)
|
||||
|
||||
process.env.NODE_ENV = 'test'
|
||||
// Has to be this because that's what the end-to-end tests expect
|
||||
process.env.PORT = `${PORT}`
|
||||
|
||||
if (await isPortRunning()) {
|
||||
console.error(`Something's already running on :${PORT}`)
|
||||
console.log(
|
||||
'If you intend to run jest tests with an existing server, set env var START_JEST_SERVER=false',
|
||||
)
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
// So it can be accessed from the script that
|
||||
// is set up by the jest config: `globalTeardown`
|
||||
global.__SERVER__ = await main()
|
||||
|
||||
console.assert(await isServerHealthy())
|
||||
} else {
|
||||
console.warn(`jest is NOT automatically starting a server on port :${PORT}`)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user