Skip to content

Commit

Permalink
chore: vitest (#2118)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladfrangu authored Oct 12, 2023
1 parent 4775d35 commit f448f81
Show file tree
Hide file tree
Showing 47 changed files with 1,529 additions and 2,204 deletions.
252 changes: 238 additions & 14 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,230 @@ sudo ifconfig lo0 alias 127.0.0.3 up
sudo ifconfig lo0 alias 127.0.0.4 up
```

## Testing in Crawlee with vitest

There are a few small differences between how testing in jest and vitest works. Mostly, they relate to what to do, and not do anymore.

### Configuration file for tests created in the package they are for

You will need to use this tsconfig.json in the `test` folder in the package (say, if you were adding a test to `packages/core` and there wasn't a `tsconfig.json` file already there)

```json
{
"extends": "../../../tsconfig.json",
"include": ["**/*", "../../**/*"],
"compilerOptions": {
"types": ["vitest/globals"]
}
}
```

### Mocking modules

Mocks are pretty much the same when it comes to jest vs vitest. One crucial difference is that you no longer need to unmock modules in an afterAll block, as they are mocked per test file.

#### Previous

```ts
jest.mock('node:os', () => {
const original: typeof import('node:os') = jest.requireActual('node:os');
return {
...original,
platform: () => 'darwin',
freemem: jest.fn(),
};
});

afterAll(() => {
jest.unmock('node:os');
});
```

#### Now

```ts
vitest.mock('node:os', async (importActual) => {
const original = await importActual<typeof import('node:os')>();
return {
...original,
platform: () => 'darwin',
freemem: jest.fn(),
};
});
```

### Mocking based on imports

Given the following two samples:

#### 1

```ts
import os from 'node:os';

console.log(os.platform());
```

#### 2

```ts
import { platform } from 'node:os';

console.log(platform());
```

You will need to mock the module based on how you import it in the source code. This means, if you will import the default export, you will need to add a `default` property to the mocked object. Otherwise, you will need to mock the module as is.

So, for example 1:

```ts
vitest.mock('node:os', async (importActual) => {
const original = await importActual<
typeof import('node:os') & { default: typeof import('node:os') }
>();

const platformMock = () => 'darwin';
const freememMock = vitest.fn();

return {
...original,
platform: platformMock,
freemem: freememMock,
// Specifically, you'll need to also mock the `default` property of the module, as seen below
default: {
...original.default,
platform: platformMock,
freemem: freememMock,
},
};
});
```

And for example 2:

```ts
vitest.mock('node:os', async (importActual) => {
const original = await importActual<typeof import('node:os')>();

const platformMock = () => 'darwin';
const freememMock = vitest.fn();

return {
...original,
platform: platformMock,
freemem: freememMock,
};
});
```

### Mocked functions

In previous jest code, we had to cast mocked functions as `jest.MockedFunction`. This is _technically_ still needed, but vitest gives us a utility function that casts it for us: `vitest.mocked()`. It doesn't do anything runtime wise, but it helps with type inference.

```ts
import os from 'node:os';

const mockedPlatform = vitest.mocked(os.platform);
```

### Resetting spies to original implementation

You no longer need to reset spies to their original implementation. This is done automatically for you via vitest's `restoreMocks` option.

With that said, if you create spies in a `beforeAll`/`beforeEach` hook, you might need to call this at the start of your file: `vitest.setConfig({ restoreMocks: false });`, as otherwise your spies will be reset before your tests run.

### Separate spy instances for methods track their own calls

In previous jest code, you could do something like this:

```ts
const spy = jest.spyOn(os, 'platform').mockReturnValueOnce('darwin');

expect(os.platform()).toBe('darwin');
expect(spy).toHaveBeenCalledTimes(1);

const spy2 = jest.spyOn(os, 'platform').mockReturnValueOnce('linux');

expect(os.platform()).toBe('linux');
expect(spy).toHaveBeenCalledTimes(2);
```

This is no longer valid in vitest. You will need to re-use the same spy instance.

```ts
const spy = vitest.spyOn(os, 'platform').mockReturnValueOnce('darwin');

expect(os.platform()).toBe('darwin');
expect(spy).toHaveBeenCalledTimes(1);

spy.mockReturnValueOnce('linux');

expect(os.platform()).toBe('linux');
expect(spy).toHaveBeenCalledTimes(2);
```

## Changing test settings

In jest, we were able to do the following to adjust timeouts at runtime:

```ts
if (os.platform() === 'win32') {
jest.setTimeout(100_000);
}
```

In vitest, you need to call the `vitest.setConfig` function instead (and specify what to change):

```ts
if (os.platform() === 'win32') {
vitest.setConfig({
testTimeout: 100_000,
});
}
```

## Hook callbacks

In jest, we were able to call the callback provided in the hooks to signal the hook has executed successfully:

```ts
beforeAll((done) => {
// Do something
done();
});
```

In vitest, this is no longer provided, but _can_ be substituted with a promise:

```ts
beforeAll(async () => {
await new Promise((resolve) => {
// Do something
resolve();
});
});
```

## `const enums`

> [!IMPORTANT]
> Certain projects, like `puppeteer` declare `const enum`s in their typings. These are enums that do not actually exist at runtime, but enums that `tsc` (which is what we're currently using to compile Crawlee) can inline the values of
> directly into the compiled code. You should avoid importing `const enums` as `vitest` will not inline them like `tsc` does and will throw an error, unless the enum is also present at runtime (check by importing the module and seeing if it's exported anywhere).
## Testing for class names in stack traces

Some tests may want to check for error stack traces and the presence of class names (a prime example is our tests for logging the stack traces for certain logger levels). In `jest`, you were able to do this:

```ts
expect(/at BasicCrawler\.requestHandler/.test(stackTrace)).toBe(true);
```

In `vitest`, at the time of writing this (2023/10/12), class names get an `_` prepended to them. In order to solve this, just add `_?` to your regular expression test (this will match both with and without the `_`).

```ts
expect(/at _?BasicCrawler\.requestHandler/.test(stackTrace)).toBe(true);
```

## Code of Conduct

### Our Pledge
Expand All @@ -52,22 +276,22 @@ orientation.
Examples of behavior that contributes to creating a positive environment
include:

* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members
- Using welcoming and inclusive language
- Being respectful of differing viewpoints and experiences
- Gracefully accepting constructive criticism
- Focusing on what is best for the community
- Showing empathy towards other community members

Examples of unacceptable behavior by participants include:

* The use of sexualized language or imagery and unwelcome sexual attention or
advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic
address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting
- The use of sexualized language or imagery and unwelcome sexual attention or
advances
- Trolling, insulting/derogatory comments, and personal or political attacks
- Public or private harassment
- Publishing others' private information, such as a physical or electronic
address, without explicit permission
- Other conduct which could reasonably be considered inappropriate in a
professional setting

### Our Responsibilities

Expand All @@ -93,7 +317,7 @@ further defined and clarified by project maintainers.
### Enforcement

Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported by contacting the project team at [email protected]. All
reported by contacting the project team at <[email protected]>. All
complaints will be reviewed and investigated and will result in a response that
is deemed necessary and appropriate to the circumstances. The project team is
obligated to maintain confidentiality with regard to the reporter of an incident.
Expand Down
33 changes: 0 additions & 33 deletions jest.config.js

This file was deleted.

17 changes: 10 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
"clean": "turbo run clean",
"build": "turbo run build && node ./scripts/typescript_fixes.mjs",
"ci:build": "turbo run build --cache-dir=\".turbo\" && node ./scripts/typescript_fixes.mjs",
"test": "jest --silent",
"test": "vitest run --silent",
"test:e2e": "node test/e2e/run.mjs",
"test:full": "cross-env CRAWLEE_DIFFICULT_TESTS=1 jest --silent",
"coverage": "jest --coverage",
"test:full": "cross-env CRAWLEE_DIFFICULT_TESTS=1 vitest run --silent",
"coverage": "vitest --coverage",
"publish:next": "lerna publish from-package --contents dist --dist-tag next --force-publish",
"release:next": "yarn build && yarn publish:next",
"publish:prod": "lerna publish from-package --contents dist --force-publish",
Expand All @@ -65,7 +65,7 @@
"@types/fs-extra": "^11.0.0",
"@types/htmlparser2": "^3.10.3",
"@types/inquirer": "^8.2.1",
"@types/jest": "^29.1.1",
"@types/is-ci": "^3.0.1",
"@types/lodash.merge": "^4.6.7",
"@types/mime-types": "^2.1.1",
"@types/node": "^18.7.13",
Expand All @@ -75,8 +75,10 @@
"@types/semver": "^7.3.12",
"@types/stream-json": "^1.7.2",
"@types/tough-cookie": "^4.0.2",
"@types/yargs": "^17.0.26",
"@typescript-eslint/eslint-plugin": "6.7.5",
"@typescript-eslint/parser": "6.7.5",
"@vitest/coverage-v8": "^0.34.6",
"apify": "*",
"basic-auth-parser": "^0.0.2",
"body-parser": "^1.20.0",
Expand All @@ -90,7 +92,7 @@
"globby": "^13.1.2",
"got": "^13.0.0",
"husky": "^8.0.1",
"jest": "^29.1.2",
"is-ci": "^3.0.1",
"lerna": "^7.0.0",
"lint-staged": "^14.0.0",
"node-gyp": "^9.1.0",
Expand All @@ -99,10 +101,11 @@
"proxy": "^1.0.2",
"puppeteer": "21.3.8",
"rimraf": "^5.0.0",
"ts-jest": "^29.0.3",
"ts-node": "^10.9.1",
"turbo": "1.10.15",
"typescript": "^5.0.0"
"typescript": "^5.0.0",
"vite-tsconfig-paths": "^4.2.1",
"vitest": "^0.34.6"
},
"packageManager": "[email protected]",
"volta": {
Expand Down
Loading

0 comments on commit f448f81

Please sign in to comment.