score:2
there is 02 options
- using
ts-mocha
the short way =>npm install --save-dev ts-mocha
mocha thin wrapper that allows running typescript tests with typescript runtime (ts-node) to get rid of compilation complexity. all mocha features are available without any limitation. see documentation.
use this ts configuration for the test tsconfig.testing.json
.
{
"compileroptions": {
"module": "commonjs",
"target": "es5"
},
"include": ["src/test/*"]
}
this a basic configuration you can update it according to your need
add script to package.json
scripts: "test-ts": "ts-mocha -p tsconfig.testing.json src/test/*"
well you should pay attention for importing text files that may run into issues. it will lead to token errors so move them to another files and make sure your
utils
functoins to be pure functions since you run unit test.
another important note: you should set
"module": "commonjs"
to fix conflict issues, like yourscannot use import statement outside a module
.
- use normal
mocha
withts-node/register
for typescript execution. check the documentation
- you should install
ts-node
andcross-env
to use the newtsconfig.testing.json
. - this is a simple configuration for the
tsconfig.testing.json
. note you should add to the up configurationmoduleresolution
tonode
check here for more details. add the path to your declaration types if there is any to avoid errors of typeerror ts2304: cannot find name "type_name"
. - add script to
package.json
scripts:cross-env ts_node_project=\"tsconfig.testing.json\" mocha -r ts-node/register src/test/*
{
"ts-node": {
"files": true
},
"compileroptions": {
"target": "es5",
"module": "commonjs",
"moduleresolution": "node"
},
"include": [
"src"
],
"exclude": [
"./node_modules/",
"./dist/"
],
"files": [
"path_to_your_types_declaration"
]
}
Source: stackoverflow.com
Related Query
- react create app, typescript unit test with mocha and chai what is the correct setup to support es6 modules?
- What is the difference between NextJs and Create React App
- Create React App with TypeScript and NPM Link: Enums causing Module parse failed
- How to check the value of a nested React component in a unit test with Enzyme and Jest
- How to determine the order of css in my create react app build with webpack and customize-cra?
- How to unit test the output of a React component method in a React Router / Redux app with Enzyme
- What is the correct way to React Hook Fetch Api and store to Redux and retrieve it with Selector immediately?
- How To Unit Test React Component With react-intl, react-router-dom v4 and TypeScript
- Unit test with enzyme, mocha, sinon and reactjs. How to see what the spy is called with?
- Create unit test with Jest and React testing library for helper which generate a domain url
- What is the correct way to load prebuilt SQlite databases with or without PouchDB in a React App
- What is the best way to build multi-language app with react and redux?
- What is the 'it' function in the test script in Create React App template?
- How to test a className with the Jest and React testing library
- What is the correct order of execution of useEffect in React parent and child components?
- StyledComponents with Typescript and ThemeProvider. What are the right types?
- Javascript: What is eslintcache file and why is always generated in create react app
- What's the easiest way to use Create React App with Relay?
- what is the correct type for a React instance of a component in typescript
- How to read console.log from a mounted component with Enzyme and Jest in Create React App
- Typescript does not error on compilation with create react app
- Connecting a component with OwnProps using react redux and typescript in the .NET Core 2.0 reactredux template project
- Test with Mocha and Enzyme a React component that uses React CSS Modules
- What is the correct typescript type for react children?
- can i use the tsx extension for test files if using react with typescript
- Can I use Create React App Jest Testing with a test reporter without ejecting?
- Unit test method that calls clearInterval with Jest and Enzyme on React
- How to test with jest and typescript with types a basic react function component
- Having an issue while up and running unit test with react redux testing library
- how to run my node application and react app in the same time with one command?
More Query from same tag
- Redux , state.concat is not a function at rootReducer. And being forced to reRender an element for it to see the state change
- Unable to modify component props from React DevTools for Chrome
- Use context from consumer in children
- Call method from view in reducer
- Block React Suspense to cover the whole page
- Animate text-shadow color left to right CSS
- How to add event listener to a component and remove it when the user click anywhere else in the window?
- Webpack doesn't recognise jsx syntax
- ag-grid (react) change rowStyle dynamically based on state
- Can't connect to twilsock while creating Client
- Performance issue with React 16.8.5 with large amount of dataset
- useContext returns an empty array
- Ajax won't send post to rails app
- How to properly change state value of array of objects?
- How can I access the redux store in mapDispachToProps?
- Nextjs Listener with callback executes itself
- Unhandled Rejection (TypeError): Cannot read property 'status' of undefined
- How to handle multiple windows in Electron application with React.JS?
- Open clicked image from array in modal
- ReactJS show loader on child component update
- Create a select option in the react js from an object
- React-spring animation only works at first render
- I'm getting a weird warning 'React hook useEffect has a missing dependency'
- When I hove on element coming from mapped list , only one object change
- Using csv file in npm version of d3 v5
- react router - don't match multiple paths (v4)
- Redux: Passing a preloadedstate with combined reducer and store enhancer
- using react: TypeError: value.products.map is not a function
- How to instantiate class which is returned from a function?
- Getting TypeError: _this2.props.handleClick is not a function when passing redux prop from container to presentational component in react