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

[Testing] Improve testing capabilities for Web #521

Merged
merged 28 commits into from
May 28, 2020
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
d1c0b50
feat(testing): add testing package
RobertBroersma May 8, 2020
a800bd2
fix(core): configure jest to work nicely with testing package
RobertBroersma May 8, 2020
82dad34
Apply suggestions from code review
RobertBroersma May 17, 2020
e91dd44
Include router mock in testing package
RobertBroersma May 17, 2020
d57e587
Merge remote-tracking branch 'origin/rb-testing' into rb-testing
RobertBroersma May 17, 2020
7b86ca9
Rely on dynamic import instead of weird import and add some comments
RobertBroersma May 17, 2020
f8a6728
Merge branch 'master' into rb-testing
RobertBroersma May 17, 2020
9b36d4a
Replace MockedProvider in render by doc explaining how to mock
RobertBroersma May 18, 2020
1ae2a66
Convert routes-auto-loader to Babel plugin
RobertBroersma May 23, 2020
a9c92c5
Move fileMock to testing package
RobertBroersma May 23, 2020
22dcc46
Add README.md according to new guidelines
RobertBroersma May 23, 2020
b2e4aff
Merge branch 'master' into rb-testing
RobertBroersma May 23, 2020
482ee70
Update and resolve @types/react and @types/react-dom
RobertBroersma May 23, 2020
0440885
Simplify import string
RobertBroersma May 25, 2020
95b6ef6
But with the .routes of course
RobertBroersma May 25, 2020
3e34a4b
And without the src
RobertBroersma May 25, 2020
c22124d
Merge branch 'master' into rb-testing
RobertBroersma May 25, 2020
f1587ca
Merge branch 'master' into rb-testing
RobertBroersma May 26, 2020
e57a239
Rename routerMock and implement all exports.
peterp May 28, 2020
0924992
Mock router.
peterp May 28, 2020
c112004
Add a description.
peterp May 28, 2020
a25e05a
Remove Apollo Mock export.
peterp May 28, 2020
abcfbd2
Add comments.
peterp May 28, 2020
9ecba60
Remove testing-library imports.
peterp May 28, 2020
edbac9e
Update README.
peterp May 28, 2020
3637d5c
Fix tests.
peterp May 28, 2020
5ea27e3
Merge pull request #1 from redwoodjs/rb-testing
RobertBroersma May 28, 2020
55b6ca4
Merge branch 'master' into rb-testing
peterp May 28, 2020
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
60 changes: 60 additions & 0 deletions packages/core/config/babel-plugin-routes-auto-loader.js
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),
])
)
),
])
),
])
)
})
},
},
},
}
}
9 changes: 9 additions & 0 deletions packages/core/config/babel-preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
* This is the babel preset used `create-redwood-app`
*/

const { getPaths } = require('@redwoodjs/internal')
const redwoodPaths = getPaths()

// TODO: Determine what to do different during development, test, and production
// TODO: Take a look at create-react-app. They've dropped a ton of knowledge.

Expand Down Expand Up @@ -132,5 +135,11 @@ module.exports = () => ({
test: /.+Cell.(js|tsx)$/,
plugins: [require('./babel-plugin-redwood-cell')],
},
// Automatically import files in `src/pages/*` in to
// the `src/Routes.[ts|jsx]` file.
{
test: redwoodPaths.web.routes,
plugins: [require('./babel-plugin-routes-auto-loader')],
},
],
})
27 changes: 27 additions & 0 deletions packages/core/config/jest.config.web.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,38 @@

const path = require('path')

const { getPaths } = require('@redwoodjs/internal')

const redwoodPaths = getPaths()

module.exports = {
resolver: 'jest-directory-named-resolver',
rootDir: process.cwd(),
globals: {
__REDWOOD__API_PROXY_PATH: '/',
},
// TODO: use getPaths().web.base instead.
setupFilesAfterEnv: [path.resolve(__dirname, './jest.setup.web.js')],
RobertBroersma marked this conversation as resolved.
Show resolved Hide resolved
moduleNameMapper: {
/**
* Make sure modules that require different versions of these
* dependencies end up using the same one.
*/
'^react$': path.resolve(redwoodPaths.base, 'node_modules', 'react'),
'^react-dom$': path.resolve(redwoodPaths.base, 'node_modules', 'react-dom'),
'^@apollo/react-common$': path.resolve(
redwoodPaths.base,
'node_modules',
'@apollo/react-common'
),

/**
* Mock out files that aren't particularly useful in tests. See fileMock.js for more info.
*/
'\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga|css)$': path.resolve(
redwoodPaths.base,
'node_modules',
'fileMock.js'
),
},
}
11 changes: 0 additions & 11 deletions packages/core/config/webpack.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,17 +157,6 @@ module.exports = (webpackEnv) => {
},
],
},
{
// Automatically import files in `src/pages/*` in to
// the `src/Routes.[ts|jsx]` file.
test: redwoodPaths.web.routes,
use: {
loader: path.resolve(
__dirname,
'../dist/loaders/routes-auto-loader.js'
),
},
},
],
},
optimization: {
Expand Down
8 changes: 6 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@babel/preset-typescript": "^7.9.0",
"@babel/runtime-corejs3": "^7.9.2",
"@prisma/cli": "2.0.0-beta.5",
"@redwoodjs/testing": "0.7.0",
"@redwoodjs/cli": "^0.7.1-canary.8",
"@redwoodjs/dev-server": "^0.7.1-canary.8",
"@redwoodjs/eslint-config": "^0.7.1-canary.8",
Expand All @@ -25,8 +26,8 @@
"@testing-library/react": "^10.0.1",
"@types/jest": "^25.1.4",
"@types/node": "^13.9.5",
"@types/react": "16.9.34",
"@types/react-dom": "16.9.7",
"@types/react": "16.9.35",
"@types/react-dom": "16.9.8",
"@types/webpack": "^4.41.11",
"babel-jest": "^25.2.3",
"babel-loader": "^8.1.0",
Expand Down Expand Up @@ -68,5 +69,8 @@
},
"devDependencies": {
"babel-plugin-tester": "^9.0.1"
},
"resolutions": {
"@types/react": "16.9.35"
}
}
1 change: 1 addition & 0 deletions packages/testing/.babelrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { extends: '../../babel.config.js' }
27 changes: 27 additions & 0 deletions packages/testing/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# PackageName

<!-- toc -->
- [Purpose and Vision](#Purpose-and-Vision)
- [Package Lead](#Package-Lead)
- [Roadmap](#Roadmap)
- [Contributing](#Contributing)
- [FAQ](#FAQ)

## Purpose and Vision
The Testing package contains a custom renderer based on `@testing-library/react`'s `render` method. All methods from this library are exposed through `@redwoodjs/testing`

## Package Lead
[@RobertBroersma](https://github.com/RobertBroersma)

## 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)


## FAQ

Answers to frequently asked questions.
21 changes: 21 additions & 0 deletions packages/testing/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "@redwoodjs/testing",
"version": "0.7.0",
"files": [
"dist"
],
"main": "dist/index.js",
"license": "MIT",
"peerDependencies": {
"@redwoodjs/router": "^0.6.0",
"react": "*"
},
"dependencies": {
"@apollo/react-testing": "^3.1.4",
"@redwoodjs/web": "^0.6.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"
}
}
6 changes: 6 additions & 0 deletions packages/testing/src/fileMock.js
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'
7 changes: 7 additions & 0 deletions packages/testing/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as routerMock from './routerMock'
export { routerMock }

// https://testing-library.com/docs/react-testing-library/setup#custom-render
export * from '@testing-library/react'
export { MockedProvider } from '@apollo/react-testing'
export { customRender as render } from './render'
RobertBroersma marked this conversation as resolved.
Show resolved Hide resolved
22 changes: 22 additions & 0 deletions packages/testing/src/render.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from 'react'
import { getPaths } from '@redwoodjs/internal'
import { render } from '@testing-library/react'

const redwoodPaths = getPaths()

const Routes = require(redwoodPaths.web.routes).default

const AllTheProviders = ({ children }) => {
return (
<>
<Routes />
{children}
</>
)
}

export const customRender = (ui, options = {}) =>
render(ui, {
wrapper: (props) => <AllTheProviders {...props} />,
...options,
})
RobertBroersma marked this conversation as resolved.
Show resolved Hide resolved
12 changes: 12 additions & 0 deletions packages/testing/src/routerMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from 'react'

export const routes = {}

export const Router = ({ children }) => {
for (let route of React.Children.toArray(children)) {
const { name } = route.props
routes[name] = jest.fn(() => name)
}

return <></>
}
Loading