Comments (5)
Unit tests would certainly be useful. Some Cypress and Typescript support is included in NX: https://blog.nrwl.io/powering-up-react-development-with-nx-cf0a9385dbec
from cypress-react-unit-test.
Added example in https://github.com/bahmutov/try-cra-app-typescript
from cypress-react-unit-test.
I'm also really looking forward to this. I've been running around in circles the last 3 days trying to get this figured out.
As soon as I add '.ts' and '.tsx' to resolve.extensions
everything falls apart even if I only have specs to components written in pure .jsx.
// ***********************************************************
// This example plugins/index.js can be used to load plugins
//
// You can change the location of this file or turn off loading
// the plugins file with the 'pluginsFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/plugins-guide
// ***********************************************************
const path = require('path');
const webpack = require('@cypress/webpack-preprocessor');
const webpackOptions = {
node: {
fs: 'empty',
},
resolve: {
extensions: [
'.ts', // <-- works fine if commented out
'.tsx', // <-- works fine if commented out
'.js',
'.jsx',
],
modules: [
'node_modules',
],
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env', '@babel/preset-react'],
plugins: ['@babel/plugin-proposal-class-properties'],
},
},
{
test: /\.(ts|tsx)$/,
loader: 'ts-loader',
exclude: [
/node_modules/,
],
},
/* {
test: /\.(ts|tsx)$/,
loader: require.resolve('babel-loader'),
options: {
presets: [
'@babel/preset-env',
'@babel/preset-react',
['react-app', { flow: false, typescript: true }]],
},
},*/
],
},
};
const options = {
// send in the options from your webpack.config.js, so it works the same
// as your app's code
webpackOptions,
watchOptions: {},
};
// This function is called when a project is opened or re-opened (e.g. due to
// the project's config changing)
module.exports = (on, config) => {
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
on('file:preprocessor', webpack(options));
};
// greeting.spec.js
/// <reference types="cypress" />
import React from 'react';
import Greeting from '../../../client/ui-library/Greeting';
describe('cypress-react-unit-test', () => {
it('shows greeting', () => {
cy.mount(<Greeting />);
cy.contains('Hello World');
});
it('changes greeting on click', () => {
cy.mount(<Greeting />);
cy.get('button').click();
cy.contains('Bonjour World');
});
});
// greeting.jsx (from some cypress.io example)
import React from 'react';
class Greeting extends React.Component {
constructor(...args) {
super(...args);
this.state = {
greeting: 'Hello',
};
}
updateGreeting() {
this.setState({
greeting: 'Bonjour',
});
}
render() {
return (
<div aria-label="greeting">
<p>
{this.state.greeting}
{' '}
World
</p>
<button onClick={this.updateGreeting.bind(this)}>
Update greeting
</button>
</div>
);
}
}
export default Greeting;
from cypress-react-unit-test.
@ETE-Jens-Ihrig I managed to put together a working example: https://github.com/ElGoorf/cypress-react-unit-test
from cypress-react-unit-test.
I had trouble finding a working example for typescript, so i put together this https://github.com/tam5/tsx-cypress-example which has a test written in .tsx
file and works.
Also, if you need hooks, check out the with-hooks
branch, which has an example of the hack needed (as of time of writing) to get that to work too.
But, it still has the problem of losing event listeners after the first test. For that we’ll have to wait I guess
from cypress-react-unit-test.
Related Issues (20)
- Create example showing how to test a component with MobX v6 HOT 1
- Error: Webpack Compilation Error HOT 1
- How one can attach to window custom fields? HOT 6
- Show example with Webpack + TypeScript + aliases HOT 1
- Move app dependencies into every folder HOT 2
- Trying to get cypress react unit testing working ... HOT 8
- Update older versions in external repositories HOT 1
- Cannot find module node_modules/get-package-type/index (in nyc module) HOT 5
- loadWebpack plugin doesn't work with terser, @rails/webpacker HOT 8
- Node V8.11.2 is not supported HOT 1
- command init crashes because of missing templates/next module HOT 1
- Error: ... please place the spec in component folder HOT 4
- Fix date-fns material date pickers in Percy example HOT 1
- Create example with Webpack 5 HOT 1
- Next.js plugin is treating the entire projectRoot as pages HOT 3
- Can't resolve 'readLine' HOT 1
- No tests found when using cypress open HOT 3
- webpack 5 and cypress-react-unit-test image assets
- [Question] Stubbing/Mocking redux hooks in mountHook HOT 4
- Conflicting preprocessors for component and BDD tests HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cypress-react-unit-test.