Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate type tests to Vitest #4731

Merged
merged 94 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
94 commits
Select commit Hold shift + click to select a range
abd23c3
Bump vitest package version to 1.3.1
aryaemami59 Mar 8, 2024
76716bf
Rename `test/tsconfig.json` to `tsconfig.test.json`
aryaemami59 Mar 8, 2024
e62ce62
Remove `test\typescript\tsconfig.json`
aryaemami59 Mar 9, 2024
efe014a
Add `tsconfig.base.json`
aryaemami59 Mar 9, 2024
783790c
Add `tsconfig.build.json` for building the library
aryaemami59 Mar 9, 2024
18f5bdb
Update `tsconfig.test.json`
aryaemami59 Mar 9, 2024
abc8dca
Update `tsconfig.json`
aryaemami59 Mar 9, 2024
7424e5c
Fix `tsup.config.ts`
aryaemami59 Mar 9, 2024
3f82265
Use type exports in `index.ts`
aryaemami59 Mar 9, 2024
5b74774
Update "check-types" script in `package.json`
aryaemami59 Mar 9, 2024
d05d737
Add types packages for Babel helper modules
aryaemami59 Mar 9, 2024
b762f50
Fix test directory in `vitest.config.mts`
aryaemami59 Mar 9, 2024
1d99459
Add include typechecking with runtime unit tests
aryaemami59 Mar 9, 2024
974bb56
Rename `actionCreators.ts` to `actionCreators.test-d.ts`
aryaemami59 Mar 9, 2024
374b644
Rename `actions.ts` to `actions.test-d.ts`
aryaemami59 Mar 9, 2024
a6ffc15
Rename `compose.ts` to `compose.test-d.ts`
aryaemami59 Mar 9, 2024
9f2247c
Rename `dispatch.ts` to `dispatch.test-d.ts`
aryaemami59 Mar 9, 2024
e7abfcc
Rename `enhancers.ts` to `enhancers.test-d.ts`
aryaemami59 Mar 9, 2024
a9de44f
Rename `injectedDispatch.ts` to `injectedDispatch.test-d.ts`
aryaemami59 Mar 9, 2024
f62250d
Rename `middleware.ts` to `middleware.test-d.ts`
aryaemami59 Mar 9, 2024
82c2312
Rename `reducers.ts` to `reducers.test-d.ts`
aryaemami59 Mar 9, 2024
38a30f3
Rename `store.ts` to `store.test-d.ts`
aryaemami59 Mar 9, 2024
6b5ee5e
Migrate type tests inside `actionCreators.test-d.ts` to Vitest
aryaemami59 Mar 9, 2024
3c645d2
Migrate type tests inside `actions.test-d.ts` to Vitest
aryaemami59 Mar 9, 2024
8169d6f
Migrate type tests inside `compose.test-d.ts` to Vitest
aryaemami59 Mar 9, 2024
3310238
Migrate type tests inside `dispatch.test-d.ts` to Vitest
aryaemami59 Mar 9, 2024
dda1f78
Migrate type tests inside `enhancers.test-d.ts` to Vitest
aryaemami59 Mar 9, 2024
4806140
Merge branch 'master' of https://github.com/reduxjs/redux into migrat…
aryaemami59 Mar 11, 2024
f6584b4
Remove unnecessary `rollup.config.js` file
aryaemami59 Mar 11, 2024
3e15164
Update CI to include correct tsconfig in `test.yaml`
aryaemami59 Mar 11, 2024
0532b69
Remove unnecessary type tests related scripts from `package.json`
aryaemami59 Mar 11, 2024
afd682f
Rename `test:types` command to `type-tests`
aryaemami59 Mar 11, 2024
2c4f862
Fix some minor type issues in `tsup.config.ts`
aryaemami59 Mar 11, 2024
21f9bb9
Migrate type tests inside `injectedDispatch.test-d.ts` to Vitest
aryaemami59 Mar 11, 2024
fbf5219
Migrate type tests inside `middleware.test-d.ts` to Vitest
aryaemami59 Mar 11, 2024
5a47003
Bump `vitest` to latest version
aryaemami59 Mar 16, 2024
942ce47
Migrate type tests inside `reducers.test-d.ts` to Vitest
aryaemami59 Mar 17, 2024
1461c18
Merge branch 'master' of https://github.com/reduxjs/redux into migrat…
aryaemami59 Mar 21, 2024
6f83aeb
Merge branch 'master' of https://github.com/reduxjs/redux into migrat…
aryaemami59 Mar 22, 2024
3d7f1a3
Fix TS issues in `tsup.config.ts`
aryaemami59 Mar 22, 2024
0bb7370
Merge branch 'master' of https://github.com/reduxjs/redux into migrat…
aryaemami59 Mar 26, 2024
f3ea8fa
Merge branch 'master' of https://github.com/reduxjs/redux into migrat…
aryaemami59 Mar 29, 2024
2ea0ff2
Merge branch 'master' of https://github.com/reduxjs/redux into migrat…
aryaemami59 Apr 10, 2024
bee7736
Merge branch 'master' of https://github.com/reduxjs/redux into migrat…
aryaemami59 Apr 15, 2024
3cca0fd
Bump `vitest`
aryaemami59 Apr 17, 2024
db1500e
Bump TypeScript
aryaemami59 Apr 17, 2024
5e1847c
Merge branch 'master' of https://github.com/reduxjs/redux into migrat…
aryaemami59 Apr 19, 2024
46aef3d
Merge branch 'master' of https://github.com/reduxjs/redux into migrat…
aryaemami59 Apr 29, 2024
60183ef
Bump `vitest` to version 1.5.3
aryaemami59 Apr 30, 2024
e5926f0
Merge branch 'master' of https://github.com/reduxjs/redux into migrat…
aryaemami59 May 3, 2024
59e6bef
Bump `vitest` to version 1.6.0
aryaemami59 May 6, 2024
2a0ac43
Merge branch 'master' of https://github.com/reduxjs/redux into migrat…
aryaemami59 May 10, 2024
b44931c
Merge branch 'master' of https://github.com/reduxjs/redux into migrat…
aryaemami59 May 30, 2024
5cb09ee
Merge branch 'master' of https://github.com/reduxjs/redux into migrat…
aryaemami59 Jun 4, 2024
dac769a
Merge branch 'master' of https://github.com/reduxjs/redux into migrat…
aryaemami59 Jun 24, 2024
74b7027
Update lockfile
aryaemami59 Jun 24, 2024
58b6e4d
Fix type issues in `tsup.config.ts`
aryaemami59 Jun 24, 2024
58526be
Fix CI tests
aryaemami59 Jun 24, 2024
df33236
Erase path aliases before running type tests
aryaemami59 Jun 24, 2024
d4a440c
Run test suite during CI
aryaemami59 Jun 24, 2024
9b2b529
Fix `build` command
aryaemami59 Jun 24, 2024
044ca73
Add `tsconfig.vitest-temp.json` to `.gitignore` file
aryaemami59 Jun 24, 2024
ca489b7
Add `.vscode` to `.gitignore`
aryaemami59 Jun 24, 2024
9a31442
Bump TypeScript to version 5.5.3
aryaemami59 Jul 9, 2024
1421cf1
Fix type issues related to TS 5.5
aryaemami59 Jul 9, 2024
06d3b70
Merge branch 'master' of https://github.com/reduxjs/redux into migrat…
aryaemami59 Jul 19, 2024
b288770
Merge branch 'master' of https://github.com/reduxjs/redux into migrat…
aryaemami59 Aug 1, 2024
bcd174d
Fix path resolution issue in `mangleErrors.cjs`
aryaemami59 Aug 1, 2024
4951352
Rename `mangleErrors.cjs` to `mangleErrors.mts`
aryaemami59 Aug 2, 2024
f7493b1
Fix type related issues in `mangleErrors.mts`
aryaemami59 Aug 2, 2024
466a5ce
Fix minor JSDoc issues
aryaemami59 Aug 2, 2024
380fc44
Add plugin name for `mangleErrorsPlugin`
aryaemami59 Aug 2, 2024
6e7360b
Export `mangleErrorsPlugin`
aryaemami59 Aug 2, 2024
8e931c8
Fix `mangleErrorsPlugin` not preserving different error types
aryaemami59 Aug 2, 2024
560e068
Reuse `mangleErrorsPlugin`'s name for consistency
aryaemami59 Aug 2, 2024
93bdabf
Remove unnecessary `examples:test` command
aryaemami59 Aug 2, 2024
b45494d
Fix `prepublishOnly` command
aryaemami59 Aug 2, 2024
395205f
Remove unnecessary `.babelrc.cjs` file
aryaemami59 Aug 2, 2024
39b680f
Remove unused `symbol-observable` import
aryaemami59 Aug 2, 2024
d669026
Update lockfile
aryaemami59 Aug 2, 2024
ea6ae59
Bump TypeScript to version 5.5.4
aryaemami59 Aug 2, 2024
2f0e521
Update lockfile
aryaemami59 Aug 6, 2024
0bae0d4
Migrate type tests inside `store.test-d.ts` to Vitest
aryaemami59 Aug 6, 2024
eff22b8
Manually define `NoInfer`
aryaemami59 Aug 6, 2024
cce0161
Update lockfile
aryaemami59 Aug 10, 2024
53d1ef1
Merge branch 'master' of https://github.com/reduxjs/redux into migrat…
aryaemami59 Aug 13, 2024
cf7482c
Update lockfile
aryaemami59 Aug 15, 2024
9cadb54
Remove duplicate `NoInfer`
aryaemami59 Aug 15, 2024
d5c0c54
Fix `.parameter` edge cases
aryaemami59 Aug 15, 2024
b272545
Remove `tslib` related settings
aryaemami59 Aug 15, 2024
2d03a71
Simplify `vitest.config.mts`
aryaemami59 Aug 15, 2024
37ee94b
Convert some default imports to wildcard imports
aryaemami59 Aug 15, 2024
d9a6bb8
Bump Vitest to version 2.0.5
aryaemami59 Aug 15, 2024
385cb33
Merge branch 'master' into migrate-typetests-to-vitest
timdorr Aug 16, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions .babelrc.cjs

This file was deleted.

17 changes: 13 additions & 4 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ jobs:
- name: Lint
run: yarn lint

- name: Run test suite
run: yarn test

- name: Build
run: yarn build

Expand Down Expand Up @@ -90,9 +93,12 @@ jobs:
- name: Install build artifact
run: yarn add ./package.tgz

- run: sed -i -e /@remap-prod-remove-line/d ./tsconfig.json ./vitest.config.mts ./test/tsconfig.json ./test/typescript/tsconfig.json
- name: Erase path aliases
run: sed -i -e /@remap-prod-remove-line/d tsconfig.base.json

- name: Run tests, against dist
env:
TEST_DIST: true
run: yarn test

test-types:
Expand Down Expand Up @@ -129,12 +135,15 @@ jobs:
- name: Install build artifact
run: yarn add ./package.tgz

- name: Erase path aliases
run: sed -i -e /@remap-prod-remove-line/d tsconfig.base.json

- name: Test types
env:
TEST_DIST: true
run: |
yarn tsc --version
yarn check-types
yarn test:typecheck
yarn test:types
yarn type-tests

are-the-types-wrong:
name: Check package config with are-the-types-wrong
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ website/translated_docs
website/build/
website/node_modules
website/i18n/*

tsconfig.vitest-temp.json

.vscode
15 changes: 7 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,20 @@
"format": "prettier --write \"{src,test}/**/*.{js,ts}\" \"**/*.md\"",
"format:check": "prettier --list-different \"{src,test}/**/*.{js,ts}\" \"**/*.md\"",
"lint": "eslint --ext js,ts src test",
"check-types": "tsc --noEmit && echo \"Types compiled\"",
"test": "vitest --run",
"test:types": "tsc -p test/typescript && echo \"Typetests passed\"",
"test": "vitest --run --typecheck",
"type-tests": "tsc -p tsconfig.test.json --noEmit",
"test:watch": "vitest --watch",
"test:cov": "vitest --coverage",
"test:typecheck": "tsc -p test && echo \"Types passed\"",
"build": "tsup",
"prepublishOnly": "yarn clean && yarn check-types && yarn format:check && yarn lint && yarn test",
"build": "yarn clean && tsup",
"prepublishOnly": "yarn clean && yarn format:check && yarn lint && yarn test",
"prepack": "yarn build",
"examples:lint": "eslint --ext js,ts examples",
"examples:test": "cross-env CI=true babel-node examples/testAll.js",
"tsc": "tsc"
},
"devDependencies": {
"@babel/core": "^7.24.3",
"@types/babel__core": "^7.20.5",
"@types/babel__helper-module-imports": "^7.18.3",
"@types/node": "^20.11.30",
"@typescript-eslint/eslint-plugin": "^7.3.1",
"@typescript-eslint/parser": "^7.3.1",
Expand All @@ -76,7 +75,7 @@
"rxjs": "^7.8.1",
"tsup": "8.0.2",
"typescript": "^5.5.4",
"vitest": "^1.4.0"
"vitest": "^2.0.5"
},
"resolutions": {
"@typescript-eslint/eslint-plugin": "7.3.1",
Expand Down
79 changes: 0 additions & 79 deletions rollup.config.js

This file was deleted.

79 changes: 57 additions & 22 deletions scripts/mangleErrors.cjs → scripts/mangleErrors.mts
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
const fs = require('fs')
const helperModuleImports = require('@babel/helper-module-imports')
import type { Node, PluginObj, PluginPass } from '@babel/core'
import * as helperModuleImports from '@babel/helper-module-imports'
import * as fs from 'node:fs'

type Babel = typeof import('@babel/core')

/**
* Converts an AST type into a javascript string so that it can be added to the error message lookup.
* Represents the options for the {@linkcode mangleErrorsPlugin}.
*
* @internal
*/
export interface MangleErrorsPluginOptions {
/**
* Whether to minify the error messages or not.
* If `true`, the error messages will be replaced with an index
* that maps object lookup.
*/
minify: boolean
}

/**
* Converts an AST type into a JavaScript string so that it can be added to the error message lookup.
*
* Adapted from React (https://github.com/facebook/react/blob/master/scripts/shared/evalToString.js) with some
* adjustments
* adjustments.
*/
const evalToString = ast => {
const evalToString = (
ast: Node | { type: 'Literal'; value: string }
): string => {
switch (ast.type) {
case 'StringLiteral':
case 'Literal': // ESLint
Expand All @@ -31,14 +50,14 @@ const evalToString = ast => {
}

/**
* Takes a `throw new error` statement and transforms it depending on the minify argument. Either option results in a
* smaller bundle size in production for consumers.
* Transforms a `throw new Error` statement based on the `minify` argument, resulting in a smaller bundle size
* for consumers in production.
*
* If minify is enabled, we'll replace the error message with just an index that maps to an arrow object lookup.
* If `minify` is enabled, the error message will be replaced with an index that maps to an object lookup.
*
* If minify is disabled, we'll add in a conditional statement to check the process.env.NODE_ENV which will output a
* an error number index in production or the actual error message in development. This allows consumers using webpack
* or another build tool to have these messages in development but have just the error index in production.
* If `minify` is disabled, a conditional statement will be added to check `process.env.NODE_ENV`, which will output
* an error number index in production or the actual error message in development. This allows consumers using Webpack
* or another build tool to have these messages in development but only the error index in production.
*
* E.g.
* Before:
Expand All @@ -49,30 +68,40 @@ const evalToString = ast => {
* throw new Error(0);
* throw new Error(1);
*
* After: (without minify):
* throw new Error(node.process.NODE_ENV === 'production' ? 0 : "This is my error message.");
* throw new Error(node.process.NODE_ENV === 'production' ? 1 : "This is a second error message.");
* After (without minify):
* throw new Error(process.env.NODE_ENV === 'production' ? 0 : "This is my error message.");
* throw new Error(process.env.NODE_ENV === 'production' ? 1 : "This is a second error message.");
*/
module.exports = babel => {
export const mangleErrorsPlugin = (
babel: Babel,
options: MangleErrorsPluginOptions
): PluginObj<PluginPass & MangleErrorsPluginOptions> => {
const t = babel.types
// When the plugin starts up, we'll load in the existing file. This allows us to continually add to it so that the
// indexes do not change between builds.
let errorsFiles = ''
if (fs.existsSync('errors.json')) {
errorsFiles = fs.readFileSync('errors.json').toString()
}
let errors = Object.values(JSON.parse(errorsFiles || '{}'))
const errors = Object.values(JSON.parse(errorsFiles || '{}'))
// This variable allows us to skip writing back to the file if the errors array hasn't changed
let changeInArray = false

return {
name: 'mangle-errors-plugin',
pre: () => {
changeInArray = false
},
visitor: {
ThrowStatement(path, file) {
ThrowStatement(path) {
if (
!('arguments' in path.node.argument) ||
!t.isNewExpression(path.node.argument)
) {
return
}
const args = path.node.argument.arguments
const minify = file.opts.minify
const { minify } = options

if (args && args[0]) {
// Skip running this logic when certain types come up:
Expand All @@ -82,11 +111,15 @@ module.exports = babel => {
path.node.argument.arguments[0].type === 'Identifier' ||
path.node.argument.arguments[0].type === 'NumericLiteral' ||
path.node.argument.arguments[0].type === 'ConditionalExpression' ||
path.node.argument.arguments[0].type === 'CallExpression'
path.node.argument.arguments[0].type === 'CallExpression' ||
!t.isExpression(path.node.argument.arguments[0]) ||
!t.isIdentifier(path.node.argument.callee)
) {
return
}

const errorName = path.node.argument.callee.name

const errorMsgLiteral = evalToString(path.node.argument.arguments[0])

if (errorMsgLiteral.includes('Super expression')) {
Expand All @@ -106,7 +139,7 @@ module.exports = babel => {
const formatProdErrorMessageIdentifier = helperModuleImports.addNamed(
path,
'formatProdErrorMessage',
'src/utils/formatProdErrorMessage',
'@internal/utils/formatProdErrorMessage',
{ nameHint: 'formatProdErrorMessage' }
)

Expand All @@ -119,13 +152,13 @@ module.exports = babel => {
if (minify) {
path.replaceWith(
t.throwStatement(
t.newExpression(t.identifier('Error'), [prodMessage])
t.newExpression(t.identifier(errorName), [prodMessage])
)
)
} else {
path.replaceWith(
t.throwStatement(
t.newExpression(t.identifier('Error'), [
t.newExpression(t.identifier(errorName), [
t.conditionalExpression(
t.binaryExpression(
'===',
Expand All @@ -150,3 +183,5 @@ module.exports = babel => {
}
}
}

export default mangleErrorsPlugin
7 changes: 7 additions & 0 deletions src/createStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ import ActionTypes from './utils/actionTypes'
import isPlainObject from './utils/isPlainObject'
import { kindOf } from './utils/kindOf'

/**
* Prevents TypeScript from inferring a generic type parameter.
*
* @template T - The type to prevent inference for.
*
* @internal
*/
type NoInfer<T> = [T][T extends any ? 0 : never]

/**
Expand Down
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import __DO_NOT_USE__ActionTypes from './utils/actionTypes'

// types
// store
export {
export type {
Dispatch,
Unsubscribe,
Observable,
Expand All @@ -21,7 +21,7 @@ export {
StoreEnhancerStoreCreator
} from './types/store'
// reducers
export {
export type {
Reducer,
ReducersMapObject,
StateFromReducersMapObject,
Expand All @@ -31,11 +31,11 @@ export {
PreloadedStateShapeFromReducersMapObject
} from './types/reducers'
// action creators
export { ActionCreator, ActionCreatorsMapObject } from './types/actions'
export type { ActionCreator, ActionCreatorsMapObject } from './types/actions'
// middleware
export { MiddlewareAPI, Middleware } from './types/middleware'
export type { MiddlewareAPI, Middleware } from './types/middleware'
// actions
export { Action, UnknownAction, AnyAction } from './types/actions'
export type { Action, UnknownAction, AnyAction } from './types/actions'

export {
createStore,
Expand Down
Loading