Browse Source

jest-test-setup

master
Timmy Huang 2 years ago
parent
commit
86667d52a1
  1. 130
      .eslintrc
  2. 1
      .gitignore
  3. 3
      babel.config.js
  4. 9
      jest-puppeteer.config.js
  5. 4955
      package-lock.json
  6. 10
      package.json
  7. 12
      puppeteer.config.js
  8. 19
      puppeteer.setup.js
  9. 20
      test/integration/zika.test.js
  10. 3
      test/sum.js
  11. 5
      test/sum.test.js

130
.eslintrc

@ -1,7 +1,16 @@
---
extends: "airbnb"
plugins: ["react-hooks"]
plugins: [
"react-hooks"
]
parser: "babel-eslint"
env:
jest: true
globals:
page: true
browser: true
context: true
jestPuppeteer: true
rules:
camelcase: off # require camel case names
prefer-template: off
@ -12,27 +21,70 @@ rules:
no-confusing-arrow: off # allow (a) => a ? 1 : 2;
no-useless-constructor: off
no-nested-ternary: off
object-curly-spacing: off # allow { a: 1 } and {a: 1}
object-curly-spacing: off # allow { a: 1
} and {a: 1
}
block-spacing: off # same as above for function blocks on a single line
dot-notation: off # allows someObject["a"] as well as someObject.a
comma-dangle: ["error", "never"]
dot-notation: off # allows someObject[
"a"
] as well as someObject.a
comma-dangle: [
"error",
"never"
]
padded-blocks: off
no-plusplus: off # we are old enought to use i++
arrow-body-style: off
arrow-parens: ["error", "always"] # use brackets always, (a) => body // () => body // (a, b) => body
no-case-declarations: off # we don't surround case blocks with { } in our switch statements as we use fallthroughs deliberately
arrow-parens: [
"error",
"always"
] # use brackets always, (a) => body // () => body // (a, b) => body
no-case-declarations: off # we don't surround case blocks with {} in our switch statements as we use fallthroughs deliberately
one-var: off
one-var-declaration-per-line: off
no-console: [1, { "allow": ["warn", "error"] }]
no-console: [
1,
{
"allow": [
"warn",
"error"
]
}
]
space-infix-ops: off
no-param-reassign: [1, { "props": false }]
no-param-reassign: [
1,
{
"props": false
}
]
no-underscore-dangle: off
no-unused-expressions: ['error', {"allowTernary": true }]
no-restricted-syntax: ['error', 'ForInStatement', 'WithStatement'] # allow ForOfStatement & LabeledStatement
no-unused-expressions: ['error',
{
"allowTernary": true
}
]
no-restricted-syntax: ['error', 'ForInStatement', 'WithStatement'
] # allow ForOfStatement & LabeledStatement
class-methods-use-this: off
react/jsx-tag-spacing: ["error", { "closingSlash": "never", "beforeSelfClosing": "allow", "afterOpening": "never" }]
react/jsx-tag-spacing: [
"error",
{
"closingSlash": "never",
"beforeSelfClosing": "allow",
"afterOpening": "never"
}
]
react/forbid-prop-types: off
react/jsx-filename-extension: [1, { "extensions": [".js", ".jsx"] }]
react/jsx-filename-extension: [
1,
{
"extensions": [
".js",
".jsx"
]
}
]
react/jsx-first-prop-new-line: off
react/no-did-mount-set-state: off
react/no-did-update-set-state: off
@ -40,23 +92,61 @@ rules:
react/sort-comp: off # possibly reinstate
jsx-a11y/no-static-element-interactions: off
import/prefer-default-export: off
no-multi-spaces: ["error", { ignoreEOLComments: true }]
no-multi-spaces: [
"error",
{ ignoreEOLComments: true
}
]
no-labels: off
no-unused-labels: off
no-continue: off
no-unneeded-ternary: ["error", { "defaultAssignment": true }]
quote-props: ["error", "as-needed"]
prefer-const: ["error", {"destructuring": "all"}]
indent: ["error", 2, {"MemberExpression": "off", "SwitchCase": 1}]
no-unneeded-ternary: [
"error",
{
"defaultAssignment": true
}
]
quote-props: [
"error",
"as-needed"
]
prefer-const: [
"error",
{
"destructuring": "all"
}
]
indent: [
"error",
2,
{
"MemberExpression": "off",
"SwitchCase": 1
}
]
react-hooks/rules-of-hooks: "error"
react-hooks/exhaustive-deps: "warn"
object-curly-newline: off
operator-linebreak: ["error", "after", { "overrides": { "?": "ignore", ":": "ignore" } }]
operator-linebreak: [
"error",
"after",
{
"overrides": {
"?": "ignore",
":": "ignore"
}
}
]
react/destructuring-assignment: off
prefer-destructuring: off
implicit-arrow-linebreak: off
jsx-quotes: off
no-use-before-define: ["error", { "functions": false }]
no-use-before-define: [
"error",
{
"functions": false
}
]
parserOptions:
ecmaVersion: 6
sourceType: module
@ -66,4 +156,4 @@ env:
es6: true
browser: true
# https://gist.github.com/odedw/cf14a55aa154adbc3bf7#file-eslintrc-yaml-L4
# https: //gist.github.com/odedw/cf14a55aa154adbc3bf7#file-eslintrc-yaml-L4

1
.gitignore

@ -7,6 +7,7 @@ big_flu
s3/
/local_narratives/
/narratives/
/datasets/
### OSX ###
.DS_Store

3
babel.config.js

@ -16,7 +16,8 @@ module.exports = function babelConfig(api) {
["@babel/plugin-proposal-decorators", { legacy: true }],
"@babel/plugin-proposal-class-properties",
"babel-plugin-styled-components",
"babel-plugin-syntax-dynamic-import"
"babel-plugin-syntax-dynamic-import",
"@babel/plugin-transform-runtime",
];
if (api.env("development")) {
if (process.env.BABEL_EXTENSION_PATH && !process.env.BABEL_EXTENSION_PATH.includes(__dirname)) {

9
jest-puppeteer.config.js

@ -0,0 +1,9 @@
module.exports = {
browserContext: 'incognito',
launch: {
dumpio: true,
headless: process.env.HEADLESS !== 'false',
slowMo: process.env.SLOWMO ? process.env.SLOWMO : 0,
ignoreHTTPSErrors: true
}
};

4955
package-lock.json
File diff suppressed because it is too large
View File

10
package.json

@ -29,7 +29,9 @@
"heroku-postbuild": "npm run build && npm run get-data && npm run get-narratives",
"rebuild-docker-image": "env bash ./scripts/rebuild-docker-image.sh",
"gzip-and-upload": "env bash ./scripts/gzip-and-upload.sh",
"build-docs": "echo 'see ./docs-src/README.md'"
"build-docs": "echo 'see ./docs-src/README.md'",
"test": "jest",
"integration-test": "NODE_ENV=test ENV=dev jest --config puppeteer.config.js"
},
"dependencies": {
"@babel/core": "^7.3.4",
@ -112,6 +114,8 @@
"yaml-front-matter": "^4.0.0"
},
"devDependencies": {
"@babel/plugin-transform-runtime": "^7.8.3",
"@babel/runtime": "^7.8.7",
"chai": "^4.1.2",
"chai-http": "^4.0.0",
"eslint": "^5.14.1",
@ -122,7 +126,9 @@
"eslint-plugin-jsx-a11y": "^5.1.1",
"eslint-plugin-react": "^7.2.1",
"eslint-plugin-react-hooks": "^1.6.0",
"mocha": "^5.2.0",
"jest": "^25.1.0",
"jest-puppeteer": "^4.4.0",
"puppeteer": "^2.1.1",
"webpack-bundle-analyzer": "^3.3.2"
}
}

12
puppeteer.config.js

@ -0,0 +1,12 @@
module.exports = {
preset: "jest-puppeteer",
globals: {
BASE_URL:
process.env.ENV === "dev"
? "http://localhost:4000"
: "https://nextstrain.org/"
},
setupFilesAfterEnv: ["expect-puppeteer", "./puppeteer.setup.js"],
testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.(tsx?|jsx?)$",
testPathIgnorePatterns: ["<rootDir>/node_modules/"]
};

19
puppeteer.setup.js

@ -0,0 +1,19 @@
import { setDefaultOptions } from 'expect-puppeteer';
// (tihuan): This is the max time a test can take to run.
// Since when debugging, we run slowMo and !headless, this means
// a test can take more time to finish, so we don't want
// jest to shut off the test too soon
jest.setTimeout(30 * 1000);
setDefaultOptions({ timeout: 3 * 1000 });
beforeEach(async () => {
await jestPuppeteer.resetBrowser();
const userAgent = await browser.userAgent();
await page.setUserAgent(`${userAgent}bot`);
await page._client.send('Animation.setPlaybackRate', { playbackRate: 12 });
await page.setViewport({
width: 1536,
height: 864
});
});

20
test/integration/zika.test.js

@ -0,0 +1,20 @@
describe("Zika", () => {
it('displays the title on the page', async () => {
await goToZikaPage()
await expect(page).toMatch("Real-time tracking of Zika virus evolution");
});
it('clicks `Play` on the `TransmissionsCard`', async () => {
await goToZikaPage()
await expect(page).toMatchElement('#TransmissionsCard')
await expect(page).toClick('button', { text: 'Play' })
await expect(page).toMatchElement('button', { text: 'Pause' })
})
});
async function goToZikaPage() {
await page.goto(`${BASE_URL}/zika?p=grid`, { waitUntil: 'networkidle2' })
}

3
test/sum.js

@ -0,0 +1,3 @@
export function sum(a, b) {
return a + b;
}

5
test/sum.test.js

@ -0,0 +1,5 @@
import { sum } from './sum';
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
Loading…
Cancel
Save