-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #521 from RobertBroersma/rb-testing
[Testing] Improve testing capabilities for Web
- Loading branch information
Showing
27 changed files
with
973 additions
and
647 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/cli/src/commands/generate/cell/__tests__/fixtures/multiWordCell.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/cli/src/commands/generate/cell/__tests__/fixtures/singleWordCell.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/cli/src/commands/generate/cell/templates/test.js.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/cli/src/commands/generate/component/__tests__/fixtures/multiWordComponent.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/cli/src/commands/generate/component/__tests__/fixtures/singleWordComponent.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/cli/src/commands/generate/component/templates/test.js.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/cli/src/commands/generate/layout/__tests__/fixtures/multiWordLayout.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/cli/src/commands/generate/layout/__tests__/fixtures/singleWordLayout.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/cli/src/commands/generate/layout/templates/test.js.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/cli/src/commands/generate/page/__tests__/fixtures/multiWordPage.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/cli/src/commands/generate/page/__tests__/fixtures/singleWordPage.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/cli/src/commands/generate/page/templates/test.js.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
const path = require('path') | ||
|
||
const { getPaths } = require('@redwoodjs/internal') | ||
const { processPagesDir } = require('@redwoodjs/internal') | ||
|
||
const redwoodPaths = getPaths() | ||
|
||
module.exports = function ({ types: t }) { | ||
// Process the dir to find all Page dependencies. | ||
let deps = processPagesDir() | ||
|
||
return { | ||
visitor: { | ||
// Remove any deps that have been explicitly declared in the Routes file. When one | ||
// is present, the user is requesting that the module be included in the main | ||
// bundle. | ||
ImportDeclaration(path) { | ||
const declaredImports = path.node.specifiers.map( | ||
(specifier) => specifier.local.name | ||
) | ||
|
||
deps = deps.filter((dep) => !declaredImports.includes(dep.const)) | ||
}, | ||
Program: { | ||
exit(nodePath) { | ||
// Prepend all imports to the top of the file | ||
deps.forEach((dep) => { | ||
const basename = path.posix.basename(dep.const) | ||
const importFile = [redwoodPaths.web.src, 'pages', basename].join( | ||
'/' | ||
) | ||
|
||
nodePath.node.body.unshift( | ||
t.variableDeclaration('const', [ | ||
t.variableDeclarator( | ||
t.identifier(dep.const), | ||
t.objectExpression([ | ||
t.objectProperty( | ||
t.identifier('name'), | ||
t.stringLiteral(dep.const) | ||
), | ||
t.objectProperty( | ||
t.identifier('loader'), | ||
t.arrowFunctionExpression( | ||
[], | ||
t.callExpression(t.identifier('import'), [ | ||
t.stringLiteral(importFile), | ||
]) | ||
) | ||
), | ||
]) | ||
), | ||
]) | ||
) | ||
}) | ||
}, | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = { extends: '../../babel.config.js' } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# @RedwoodJS/Testing | ||
|
||
<!-- toc --> | ||
- [Purpose and Vision](#Purpose-and-Vision) | ||
- [Package Lead](#Package-Lead) | ||
- [Roadmap](#Roadmap) | ||
- [Contributing](#Contributing) | ||
|
||
## Purpose and Vision | ||
|
||
This package provides helpful defaults when testing a Redwood project's web side. The core of the project is an re-export of `@testing-library/react` with a custom `render` method. | ||
|
||
## Usage | ||
|
||
In a jest test: | ||
```js | ||
import { render, screen } from '@redwoodjs/testing | ||
it('works as expected', () => { | ||
render(<MyComponent />) | ||
expect(screen.queryByText('Text in my component')).toBeInTheDocument() | ||
} | ||
``` | ||
## Package Lead | ||
- [@RobertBroersma](https://github.com/RobertBroersma) | ||
- [@peterp](https://github.com/peterp) | ||
## Roadmap | ||
See [[Testing] Support Jest --config extensibility](https://github.com/redwoodjs/redwood/issues/564) | ||
## Contributing | ||
Core technologies | ||
- [Jest](https://jestjs.io/docs/en/getting-started) | ||
- [React Testing Library](https://testing-library.com/docs/react-testing-library/intro) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "@redwoodjs/testing", | ||
"description": "Tools, wrappers and defaults for testing a Redwood project.", | ||
"version": "0.7.0", | ||
"files": [ | ||
"dist" | ||
], | ||
"main": "dist/index.js", | ||
"license": "MIT", | ||
"peerDependencies": { | ||
"@redwoodjs/router": "*", | ||
"react": "*" | ||
}, | ||
"dependencies": { | ||
"@redwoodjs/internal": "^0.7.0", | ||
"@redwoodjs/web": "^0.7.0", | ||
"@testing-library/react": "^10.0.4" | ||
}, | ||
"scripts": { | ||
"build": "yarn cross-env NODE_ENV=production babel src -d dist --delete-dir-on-start --extensions \".js,.ts\" --source-maps inline", | ||
"build:watch": "nodemon --watch src --ext 'js,ts,tsx' --ignore dist --exec 'yarn build'" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import React from 'react' | ||
// We're bypassing the `main` field in `package.json` because we're | ||
// replacing imports of `@redwoodjs/router` with this file, and not doing so | ||
// would cause an infinite loop. | ||
// See: ./packages/core/config/jest.config.web.js | ||
export * from '@redwoodjs/router/dist/index' | ||
|
||
export const routes = {} | ||
|
||
/** | ||
* This is used in place of the real router during tests. | ||
* It populates the `routes.<pagename>()` utility object. | ||
*/ | ||
export const Router = ({ children }) => { | ||
for (let route of React.Children.toArray(children)) { | ||
const { name } = route.props | ||
routes[name] = jest.fn(() => name) | ||
} | ||
return <></> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** | ||
* configure Jest to gracefully handle asset files such as stylesheets and images. | ||
* Usually, these files aren't particularly useful in tests so we can safely mock them out. | ||
* https://jestjs.io/docs/en/webpack#handling-static-assets | ||
*/ | ||
module.exports = 'fileMock' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export { MockRouter } from './MockRouter' | ||
|
||
// https://testing-library.com/docs/react-testing-library/setup#custom-render | ||
export * from '@testing-library/react' | ||
export { customRender as render } from './render' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import React from 'react' | ||
import { getPaths } from '@redwoodjs/internal' | ||
import { render } from '@testing-library/react' | ||
|
||
// Import the user's Router from `./web/src/Router.js`. | ||
// We use the `children` from this Router that are rendered via | ||
// `./MockRouter.Router` so that we can populate the `routes object`. | ||
const { default: UserRouterWithRoutes } = require(getPaths().web.routes) | ||
|
||
const AllTheProviders = ({ children }) => { | ||
return ( | ||
<> | ||
<UserRouterWithRoutes /> | ||
{children} | ||
</> | ||
) | ||
} | ||
|
||
export const customRender = (ui, options = {}) => | ||
render(ui, { | ||
wrapper: (props) => <AllTheProviders {...props} />, | ||
...options, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.