Gathering detailed insights and metrics for nightwatch-react-fixes
Gathering detailed insights and metrics for nightwatch-react-fixes
Gathering detailed insights and metrics for nightwatch-react-fixes
Gathering detailed insights and metrics for nightwatch-react-fixes
npm install nightwatch-react-fixes
Typescript
Module System
Node Version
NPM Version
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
6
3
6
Official Nightwatch plugin which adds component testing support for React apps. It uses the Vite dev server under the hood and vite-plugin-nightwatch. Requires Nightwatch 2.4+
npm install @nightwatch/react
Update your Nightwatch configuration and add the plugin to the list:
1module.exports = { 2 plugins: ['@nightwatch/react'] 3 4 // other nightwatch settings... 5}
If you already have a Vite project, then the @nightwatch/react
plugin will try to use the existing vite.config.js
or vite.config.ts
, if either one is found.
Check the vite-plugin-nightwatch project for more configuration options.
Update the vite.config.js
and add the vite-plugin-nightwatch
plugin:
1// vite.config.js 2 3import nightwatchPlugin from 'vite-plugin-nightwatch' 4 5export default { 6 plugins: [ 7 // ... other plugins, such as vue() or react() 8 nightwatchPlugin() 9 ] 10})
We’ve designed the @nightwatch/react
plugin to work with sensible configuration defaults, but in some more advanced scenarios you may need to change some of the config options.
By default, Nightwatch will attempt to start the Vite dev server automatically. You can disable that by adding the below in your nightwatch.conf.js
file, under the vite_dev_server
dictionary.
This is common to other component testing plugins that are based on Vite, such as the @nightwatch/vue
plugin.
1// nightwatch.conf.js 2 3module.exports = { 4 plugins: ['@nightwatch/react'], 5 vite_dev_server: { 6 start_vite: true, 7 port: 5173 8 } 9}
The plugin accepts a few config options which can be set when working with an existing vite.config.js
file in the project.
renderPage
Specify the path to a custom test renderer to be used. A default renderer is included in the package, but this option can overwrite that value.
1// vite.config.js 2 3export default { 4 plugins: [ 5 // ... other plugins, such as vue() or react() 6 nightwatchPlugin({ 7 renderPage: './src/test_renderer.html' 8 }) 9 ] 10}
This plugin includes a few Nightwatch commands which can be used while writing React component tests.
componentPath
, [props]
, [callback]
):Parameters:
componentPath
– location of the component file (.jsx
) to be mountedprops
– properties to be passed to the React component, this will be serialized to JSON; an inline string can be also usedcallback
– an optional callback function which will be called with the component element1const component = await browser.mountComponent('/src/components/Form.jsx')
1const component = await browser.mountComponent('/src/components/Form.jsx', {
2 prop: 'value'
3})
A function or a string can be used for passing props that cannot be serialized. If a function is passed, it will be sent as a toString()
value.
1const component = await browser.mountComponent('/src/components/UserInfo.jsx', function () { 2 return { 3 date: new Date(), 4 text: 'I hope you enjoy reading Ulysses!', 5 author: { 6 name: 'Leopold Bloom', 7 avatarUrl: 'https://upload.wikimedia.org/wikipedia/commons/5/52/Poldy.png' 8 } 9 } 10});
This will call browser.navigateTo('/test_renderer/')
and open the browser. Needs to be used before the .importScript()
command, if used.
You can also set launchUrl
as a global at runtime and then the url to be used will be ${browser.globals.launchUrl}/test_renderer
, which makes it possible to set the launch url dynamically.
scriptPath
, [options]
, [callback]
):Parameters:
scriptPath
– location of the script file to inject into the page which will render the component; needs to be written in ESM formatoptions
– this can include:
scriptType
: the type
attribute to be set on the <script>
tag (default is module
)componentType
: should be set to 'react'
callback
– an optional callback function which will be called with the component element1const formComponent = await browser
2 .launchComponentRenderer()
3 .importScript('/test/lib/scriptToImport.js');
Example scriptToImport.js
:
1import React from 'react' 2import ReactDOM from 'react-dom' 3import Component from '/test/components/Form.jsx' 4 5const element = React.createElement(Component, {}); 6ReactDOM.render(element, document.getElementById('app')); 7 8// This will be used by Nightwatch to inspect properties of this component 9window['@@component_element'] = element;
Debugging component tests in Nightwatch isn't as straightforward as debugging a regular Node.js application or service, since Nightwatch needs to inject the code to render to component into the browser.
However, since Nightwatch v2.4 we provide several ways to inspect and debug the mounted component using the browser devtools console. Refer to the guide page on our docs website for more details: https://nightwatchjs.org/guide/component-testing/debugging.html
1npx nightwatch test/src/userInfoTest.js --devtools --debug
Tests for this project are written in Nightwatch so you can inspect them as examples, located in the [test/src] folder.
Run them with::
1npm test
We've put together a basic To-do app written in React and built on top of Vite which can be used as a boilerplate. It can be found at nightwatchjs-community/todo-react.
MIT
No vulnerabilities found.
No security vulnerabilities found.