diff --git a/.travis.yml b/.travis.yml index 609516916..831379f15 100644 --- a/.travis.yml +++ b/.travis.yml @@ -27,6 +27,6 @@ before_install: - npm prune script: - - npm run test-with-coverage && npm run docs-webpack-2-test + - npm run test && npm run docs-webpack-2-test && npm run docs-webpack-2-reset after_success: npm run coverage diff --git a/__tests__/README.md b/__tests__/README.md deleted file mode 100644 index 0d9bf3fa3..000000000 --- a/__tests__/README.md +++ /dev/null @@ -1,9 +0,0 @@ -# Integration tests - -This folder is for integration tests only. - -## UNIT TESTS ARE IN EACH MODULES IN ``src_`` - -See `src/**/__tests__/*.js` for unit tests. - -Tests here need to be run with a valid `test-phenomic-theme-base` folder. diff --git a/__tests__/build-result.js b/__tests__/build-result.js deleted file mode 100644 index f2690549a..000000000 --- a/__tests__/build-result.js +++ /dev/null @@ -1,102 +0,0 @@ -import { readFileSync } from "fs" -import { join } from "path" - -import test from "jest-ava-api" -import globby from "globby" - -const testFolder = __dirname + "/../test-phenomic-theme-base/dist" -const files = globby.sync("**/*", { - cwd: testFolder, - nodir: true, -}) - -test("should have a CSS file", (t) => { - t.is( - files.filter( - (file) => file.startsWith("phenomic.browser.") && file.endsWith(".css") - ).length, - 1 - ) -}) - -test("should have html files", (t) => { - const htmlFiles = files.filter( - (file) => file.endsWith(".html") - ).sort() - - const includes = (file, strings) => { - const content = readFileSync( - join(testFolder, file), - { encoding: "utf8" } - ) - return strings.reduce((acc, s) => { - const isTheStringInTheContent = content.includes(s) - if (!isTheStringInTheContent) { - console.log(s + " is not in " + file) - } - return acc && isTheStringInTheContent - }, true) - } - - t.deepEqual( - htmlFiles, - [ - "404.html", - "index.html", - "loading/index.html", - "posts/first-post/index.html", - "posts/hello-world/index.html", - ] - ) - - // same tests for all pages - t.truthy(includes( - "404.html", - [ - "window.__COLLECTION__ = " + - "[{\"layout\":\"PageError\",\"route\":\"404.html\",\"description\"", - ] - )) - - // ensure all pages have the correct title - - t.truthy(includes( - "404.html", - [ - ">😱 Oooops", - "window.__INITIAL_STATE__ = {\"pages\":{\"/404.html\"", - ] - )) - - t.truthy(includes( - "index.html", - [ - ">Phenomic base theme", - "window.__INITIAL_STATE__ = {\"pages\":{\"/\"", - ] - )) - - t.truthy(includes( - "loading/index.html", - [ - ">Loading...", - "window.__INITIAL_STATE__ = {\"pages\":{\"/loading/\"", - ] - )) - - t.truthy(includes( - "posts/first-post/index.html", - [ - ">First Post", - "window.__INITIAL_STATE__ = {\"pages\":{\"/posts/first-post/\"", - ] - )) - - t.truthy(includes( - "posts/hello-world/index.html", - [ - ">Hello World!", - "window.__INITIAL_STATE__ = {\"pages\":{\"/posts/hello-world/\"", - ] - )) -}) diff --git a/__tests__/cli.js b/__tests__/cli.js deleted file mode 100644 index 3b926b841..000000000 --- a/__tests__/cli.js +++ /dev/null @@ -1,87 +0,0 @@ -import { join } from "path" - -import { exec } from "child_process" - -const target = join(__dirname, "..", "test-phenomic-theme-base") -const execOpts = { cwd: target } - -const phenomic = "node ./node_modules/.bin/phenomic" -const timing = process.env.CI ? 10000 : 5000 - -describe("Integration: CLI", () => { - beforeEach(() => { - process.env.BABEL_ENV = "development" - }) - - it("should throw if a CLI flag is NOT recognized", () => { - return new Promise((resolve, reject) => { - const child = exec( - `${ phenomic } start --open=false --lol`, execOpts, - (err) => { - if (err && !err.killed) { - clearTimeout(timeout) - expect(err.message.indexOf("Unknown argument") > -1).toBeTruthy() - resolve() - } - } - ) - - const timeout = setTimeout(() => { - child.kill() - reject("Test didn't finish before timeout") - }, timing) - }) - }) - - it("should NOT throw if a CLI flag is recognized", () => { - return new Promise((resolve, reject) => { - const child = exec( - `${ phenomic } start --open=false --devPort=4000`, execOpts, - - // should die quickly... - (err) => { - if (err && !err.killed) { - clearTimeout(timeout) - reject(err) - } - } - ) - - // ...or be ok quickly - // we assume it's ok and kill the process - // we don't need the actual build - const timeout = setTimeout(() => { - child.kill() - resolve() - }, timing) - }) - }) - - it("should NOT throw if port is used", () => { - return new Promise((resolve, reject) => { - const app = require("express")() - const server = app.listen(3333, (err) => { - if (err) { - reject(err) - } - - const child = exec( - `${ phenomic } start --open=false --devPort=3333`, execOpts, - - (err) => { - if (err && !err.killed) { - clearTimeout(timeout) - reject(err) - } - } - ) - - const timeout = setTimeout(() => { - child.kill() - server.close() - resolve() - }, timing * 2) - }) - }) - }) -}) diff --git a/__tests__/jest-config.json b/__tests__/jest-config.json deleted file mode 100644 index 7ab875b0c..000000000 --- a/__tests__/jest-config.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "testRegex": "__tests__/.*\\.js$", - "setupTestFrameworkScriptFile": "/scripts/jest-framework-setup.js", - "testPathDirs": [ - "__tests__" - ] -} diff --git a/e2e-tests/README.md b/e2e-tests/README.md new file mode 100644 index 000000000..93a8e1542 --- /dev/null +++ b/e2e-tests/README.md @@ -0,0 +1,14 @@ +# End to end tests + +End-to-end testing is a methodology used to test whether the flow of an +application is performing as designed from start to finish. + +Tests here need to be run with a transpiled sources & a test folder ready to run + +```console +npm run transpile +npm run test-phenomic-theme-base:prepare +npm run e2e:tests +``` + +Otherwise you can just use `npm test` to run every tests. diff --git a/e2e-tests/__tests__/cli-arg-port.js b/e2e-tests/__tests__/cli-arg-port.js new file mode 100644 index 000000000..52c7a332e --- /dev/null +++ b/e2e-tests/__tests__/cli-arg-port.js @@ -0,0 +1,35 @@ +import { + exec, + execOpts, + phenomic, + timing, + maxTimeout, +} from "./utils" + +it("should NOT throw if port is used", +() => new Promise((resolve, reject) => { + const app = require("express")() + const server = app.listen(3333, (err) => { + if (err) { + reject(err) + } + + const child = exec( + `${ phenomic } start --open=false --devPort=3333`, execOpts, + + (err) => { + if (err && !err.killed) { + clearTimeout(timeout) + reject(err) + } + } + ) + + // server need to be killed manually + const timeout = setTimeout(() => { + server.close() + child.kill() + resolve() + }, timing) + }) +}), maxTimeout) diff --git a/e2e-tests/__tests__/cli-args-know.js b/e2e-tests/__tests__/cli-args-know.js new file mode 100644 index 000000000..d6687a267 --- /dev/null +++ b/e2e-tests/__tests__/cli-args-know.js @@ -0,0 +1,30 @@ +import { + exec, + execOpts, + phenomic, + timing, + maxTimeout, +} from "./utils" + +it("should NOT throw if a CLI flag is recognized", +() => new Promise((resolve, reject) => { + const child = exec( + `${ phenomic } start --open=false --devPort=4000`, execOpts, + + // should die quickly... + (err) => { + if (err && !err.killed) { + clearTimeout(timeout) + reject(err) + } + } + ) + + // ...or be ok quickly + // we assume it's ok and kill the process + // we don't need the actual build + const timeout = setTimeout(() => { + child.kill() + resolve() + }, timing) +}), maxTimeout) diff --git a/e2e-tests/__tests__/cli-args-unknow.js b/e2e-tests/__tests__/cli-args-unknow.js new file mode 100644 index 000000000..7164d05ac --- /dev/null +++ b/e2e-tests/__tests__/cli-args-unknow.js @@ -0,0 +1,35 @@ +import { + exec, + execOpts, + phenomic, + timing, + maxTimeout, +} from "./utils" + +it("should throw if a CLI flag is NOT recognized", +() => new Promise((resolve, reject) => { + const child = exec( + `${ phenomic } start --open=false --lol`, execOpts, + (err) => { + if (err && !err.killed) { + clearTimeout(timeout) + const isUnknown = err.message.indexOf("Unknown argument") > -1 + if (isUnknown) { + resolve() + return + } + console.log(err) + } + reject() + } + ) + + // server need to be killed manually + const timeout = setTimeout( + () => { + child.kill() + reject("Test didn't finish before timeout") + }, + timing + ) +}), maxTimeout) diff --git a/e2e-tests/__tests__/cli-build.js b/e2e-tests/__tests__/cli-build.js new file mode 100644 index 000000000..a8d974a67 --- /dev/null +++ b/e2e-tests/__tests__/cli-build.js @@ -0,0 +1,111 @@ + +import globby from "globby" + +import { + read, + exec, + execOpts, + phenomic, + maxTimeout, +} from "./utils" + +it("should build the base theme correctly", +() => new Promise((resolve, reject) => { + const child = exec( + `${ phenomic } build --cache`, execOpts, + (err) => { + if (err && !err.killed) { + reject("Build should work") + } + + const testFolder = __dirname + "/../../test-phenomic-theme-base/dist" + const files = globby.sync("**/*", { + cwd: testFolder, + nodir: true, + }) + + expect( + files.length + ) + .toBeGreaterThan( + 0 + ) + + // should have a CSS file + expect( + files.filter((file) => + file.startsWith("phenomic.browser.") && file.endsWith(".css") + ).length + ) + .toEqual( + 1 + ) + + // should have html files + expect( + files.filter((f) => f.endsWith(".html")).sort() + ) + .toEqual( + [ + "404.html", + "index.html", + "loading/index.html", + "posts/first-post/index.html", + "posts/hello-world/index.html", + ] + ) + + // same tests for all pages + const html404 = read(testFolder, "404.html") + expect(html404).toContain( + "window.__COLLECTION__ = " + + "[{\"layout\":\"PageError\",\"route\":\"404.html\",\"description\"" + ) + + // ensure all pages have the correct title + expect(html404).toContain( + ">😱 Oooops" + ) + + expect(html404).toContain( + "window.__INITIAL_STATE__ = {\"pages\":{\"/404.html\"", + ) + + const htmlIndex = read(testFolder, "index.html") + expect(htmlIndex).toContain( + ">Phenomic base theme", + ) + + expect(htmlIndex).toContain( + "window.__INITIAL_STATE__ = {\"pages\":{\"/\"", + ) + + const htmlLoading = read(testFolder, "loading/index.html") + expect(htmlLoading).toContain( + ">Loading..." + ) + expect(htmlLoading).toContain( + "window.__INITIAL_STATE__ = {\"pages\":{\"/loading/\"", + ) + + const htmlFirstPost = read(testFolder, "posts/first-post/index.html") + expect(htmlFirstPost).toContain( + ">First Post" + ) + expect(htmlFirstPost).toContain( + "window.__INITIAL_STATE__ = {\"pages\":{\"/posts/first-post/\"", + ) + + const htmlHello = read(testFolder, "posts/hello-world/index.html") + expect(htmlHello).toContain( + ">Hello World!" + ) + expect(htmlHello).toContain( + "window.__INITIAL_STATE__ = {\"pages\":{\"/posts/hello-world/\"", + ) + + child.kill() + resolve() + } + ) +}), maxTimeout * 10) diff --git a/e2e-tests/__tests__/utils/index.js b/e2e-tests/__tests__/utils/index.js new file mode 100644 index 000000000..c0e67f310 --- /dev/null +++ b/e2e-tests/__tests__/utils/index.js @@ -0,0 +1,17 @@ +import { join } from "path" +import { readFileSync } from "fs" + +export { exec } from "child_process" + +const target = join( + __dirname, "..", "..", "..", "test-phenomic-theme-base" +) + +export const execOpts = { cwd: target, stdio: "inherit" } +export const phenomic = "node ./node_modules/.bin/phenomic" +export const timing = process.env.CI ? 10000 : 5000 +export const maxTimeout = process.env.CI ? 20000 : 10000 +export const read = (testFolder, file) => readFileSync( + join(testFolder, file), + { encoding: "utf8" } +) diff --git a/integration-tests/jest-config.json b/integration-tests/jest-config.json deleted file mode 100644 index 7ab875b0c..000000000 --- a/integration-tests/jest-config.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "testRegex": "__tests__/.*\\.js$", - "setupTestFrameworkScriptFile": "/scripts/jest-framework-setup.js", - "testPathDirs": [ - "__tests__" - ] -} diff --git a/package.json b/package.json index a69d8faf5..2018c696c 100644 --- a/package.json +++ b/package.json @@ -106,7 +106,7 @@ "expect": "^1.13.4", "expect-jsx": "^2.2.1", "flow-bin": "^0.32.0", - "jest": "^15.1.1", + "jest": "^16.0.2", "jest-ava-api": "^0.1.0", "js-beautify": "^1.5.10", "jsdom": "^9.2.0", @@ -152,35 +152,31 @@ "lint:js:flow": "flow check", "lint:js": "npm-run-all --parallel lint:js:*", "lint": "npm-run-all --parallel lint:*", - "tests": "cross-env TESTING=1 jest", - "#integration-tests": "needs this order (index test the build, and cli is cleaning it)", - "integration-tests": "jest --config __tests__/jest-config.json", + "tests": "cross-env TESTING=1 jest --runInBand --coverage src", + "e2e:tests": "jest --runInBand --forceExit e2e-tests", "phenomic-theme-base": "babel-node scripts/phenomic-theme-base.js", "phenomic-theme-base-linting": "eslint --config package.json --ignore-path .gitignore --fix themes/phenomic-theme-base/scripts themes/phenomic-theme-base/src", "prephenomic-theme-base-start": "npm run transpile && npm run phenomic-theme-base", "phenomic-theme-base-start": "cd themes/phenomic-theme-base && npm start", - "pretest-phenomic-theme-base": "babel-node scripts/test-phenomic-theme-base.js", + "test-phenomic-theme-base:prepare": "babel-node scripts/test-phenomic-theme-base.js", + "pretest-phenomic-theme-base": "npm run test-phenomic-theme-base:prepare", "test-phenomic-theme-base": "cd test-phenomic-theme-base && npm test", - "posttest-phenomic-theme-base": "npm run integration-tests", "docs": "babel-node scripts/docs.js", "docs-linting": "eslint --ignore-path .gitignore --fix docs/scripts docs/src", "docs-webpack-2": "npm i webpack@^2.1.0-beta.25 && babel-node scripts/docs.js && cd docs && npm i extract-text-webpack-plugin@^2.0.0-beta.4", "docs-webpack-2-start": "npm run docs-webpack-2 && npm start", "docs-webpack-2-test": "npm run docs-webpack-2 && cd docs && npm test", + "docs-webpack-2-reset": "npm install && npm run docs", "pretest-docs": "npm run docs && npm run docs-linting", "test-docs": "cd docs && npm test", - "#posttest-docs": "install back webpack 1", - "posttest-docs": "npm install && npm run docs", "predocs-start": "npm run docs", "docs-start": "cd docs && npm start", "predocs-deploy": "cd docs && npm run showcase-screenshots && npm run build", "docs-deploy": "cd docs && ./scripts/deploy.sh", - "preall-tests": "npm run transpile && npm run lint", - "all-tests": "cross-env TESTING=1 npm run tests && npm run phenomic-theme-base && npm run phenomic-theme-base-linting && npm run test-phenomic-theme-base", - "test": "npm run all-tests", + "pretests": "npm run transpile && npm run lint", + "test": "npm run tests && npm run phenomic-theme-base && npm run phenomic-theme-base-linting && npm run test-phenomic-theme-base && npm run e2e:tests", "posttest": "npm run test-docs", - "test-with-coverage": "nyc --check-coverage --lines 70 npm run all-tests", - "coverage": "coveralls", + "coverage": "cat ./coverage/lcov.info | coveralls", "release": "npmpub", "postrelease": "npm run docs-deploy" }, @@ -220,19 +216,25 @@ "extends": "stylelint-config-standard" }, "jest": { + "testEnvironment": "node", + "notify": true, + "coverageThreshold": { + "global": { + "branches": 65, + "functions": 65, + "lines": 65, + "statements": 65 + } + }, "preprocessorIgnorePatterns": [ "node_modules" ], "testPathIgnorePatterns": [ + "/utils/", "/fixtures/", "/results/", "/stub/", "/_output/" - ], - "testPathDirs": [ - "src" - ], - "setupTestFrameworkScriptFile": "/scripts/jest-framework-setup.js", - "testEnvironment": "node" + ] } } diff --git a/scripts/jest-framework-setup.js b/scripts/jest-framework-setup.js deleted file mode 100644 index 8f631e794..000000000 --- a/scripts/jest-framework-setup.js +++ /dev/null @@ -1,2 +0,0 @@ -// eslint-disable-next-line -jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000 // 30s diff --git a/src/_utils/error-formatter/__tests__/index.js b/src/_utils/error-formatter/__tests__/index.js index 71a4dab28..d0bc56824 100644 --- a/src/_utils/error-formatter/__tests__/index.js +++ b/src/_utils/error-formatter/__tests__/index.js @@ -2,7 +2,7 @@ import path from "path" -import test from "ava" +import test from "jest-ava-api" import multili from "multili" import errorFormatter from "../index.js" diff --git a/src/bin/__tests__/check-engine.js b/src/bin/__tests__/check-engine.js index c6414b800..a0b6dde1d 100644 --- a/src/bin/__tests__/check-engine.js +++ b/src/bin/__tests__/check-engine.js @@ -5,31 +5,25 @@ import test from "jest-ava-api" import checkEngine from "../check-engine" test("should not throw when sastifies", (t) => { - process.env.FAKE_NODE_VERSION = "6.0.0" - process.env.FAKE_NPM_VERSION = "3.0.0" - t.notThrows(checkEngine) + t.notThrows( + () => checkEngine("6.0.0", "3.0.0"), + ) }) test("should throw", (t) => { - process.env.FAKE_NODE_VERSION = "3.0.0" - process.env.FAKE_NPM_VERSION = "2.0.0" t.throws( - checkEngine, + () => checkEngine("3.0.0", "2.0.0"), (error) => error.message.includes("node version is 3.0.0"), "when node version doesn't sastify" ) - process.env.FAKE_NODE_VERSION = "4.2.0" - process.env.FAKE_NPM_VERSION = "2.0.0" + t.throws( - checkEngine, + () => checkEngine("4.2.0", "2.0.0"), (error) => error.message.includes("npm version is 2.0.0"), "when npm version doesn't sastify" ) - - process.env.FAKE_NODE_VERSION = "3.0.0" - process.env.FAKE_NPM_VERSION = "2.0.0" t.throws( - checkEngine, + () => checkEngine("3.0.0", "2.0.0"), (error) => ( error.message.includes("node version is 3.0.0") && error.message.includes("npm version is 2.0.0") diff --git a/src/bin/check-engine.js b/src/bin/check-engine.js index b80a38863..720a209a5 100644 --- a/src/bin/check-engine.js +++ b/src/bin/check-engine.js @@ -5,16 +5,12 @@ import colors from "chalk" import pkg from "../../package.json" -module.exports = function() { +module.exports = function(nodeVersion, npmVersion) { const requirements = pkg.engines - const nodeVersion = process.env.TESTING - ? process.env.FAKE_NODE_VERSION - : process.version + nodeVersion = (nodeVersion || process.version) const npm = /^win/.test(process.platform) ? "npm.cmd" : "npm" - const npmVersion = process.env.TESTING ? - process.env.FAKE_NPM_VERSION : - execSync(npm + " --version").toString().trim() + npmVersion = (npmVersion || execSync(npm + " --version").toString().trim()) if (!( semver.satisfies(nodeVersion, requirements.node) && diff --git a/src/components/PageContainer/__tests__/__snapshots__/component.js.snap b/src/components/PageContainer/__tests__/__snapshots__/component.js.snap index cbd95bec7..1284ddd90 100644 --- a/src/components/PageContainer/__tests__/__snapshots__/component.js.snap +++ b/src/components/PageContainer/__tests__/__snapshots__/component.js.snap @@ -12,7 +12,7 @@ available 1`] = `

diff --git a/src/configurator/__tests__/index.js b/src/configurator/__tests__/index.js index f1002fe07..329eb408b 100644 --- a/src/configurator/__tests__/index.js +++ b/src/configurator/__tests__/index.js @@ -4,6 +4,8 @@ import test from "jest-ava-api" import configurator from ".." +const currentNodeEnv = process.env.NODE_ENV + test("should return a default configuration", (t) => { const config = configurator() @@ -49,6 +51,8 @@ test("should return a default configuration", (t) => { config, expected ) + + process.env.NODE_ENV = currentNodeEnv }) test("should allow to override some default values", (t) => { @@ -65,10 +69,11 @@ test("should allow to override some default values", (t) => { t.is(config.CNAME, true) t.is(config.devPort, 2000) t.is(config.devHost, "1.2.3.4") + + process.env.NODE_ENV = currentNodeEnv }) test("should warn if config is invalid", (t) => { - process.env.TESTING = "1" t.throws( () => configurator({ pkg: { @@ -79,10 +84,11 @@ test("should warn if config is invalid", (t) => { }), (error) => error.message.includes("Unknow option 'lol'.") ) + + process.env.NODE_ENV = currentNodeEnv }) test("should warn if config is invalid when '--production' is used", (t) => { - process.env.TESTING = "1" t.throws( () => { configurator({ @@ -91,6 +97,8 @@ test("should warn if config is invalid when '--production' is used", (t) => { }, (error) => error.message.includes("Your package.json require a 'homepage'") ) + + process.env.NODE_ENV = currentNodeEnv }) test("should not warn if config is valid when '--production' is used", (t) => { @@ -100,6 +108,8 @@ test("should not warn if config is valid when '--production' is used", (t) => { argv: [ "--production" ], }) t.is(config.baseUrl.href, "http://te.st/") + + process.env.NODE_ENV = currentNodeEnv }) test("should adjust 'NODE_ENV' when '--production' is used", (t) => { @@ -109,4 +119,6 @@ test("should adjust 'NODE_ENV' when '--production' is used", (t) => { argv: [ "--production" ], }) t.is(process.env.NODE_ENV, "production") + + process.env.NODE_ENV = currentNodeEnv }) diff --git a/src/configurator/index.js b/src/configurator/index.js index 1a2f17533..e600ed98c 100644 --- a/src/configurator/index.js +++ b/src/configurator/index.js @@ -61,10 +61,9 @@ export default function config({ argv = [], pkg = {} } = {}) { if (process.env.TESTING) { throw new Error(errorMessage) } - else { - console.error(errorMessage) - process.exit(1) - } + // else + console.error(errorMessage) + process.exit(1) } return config diff --git a/src/loader-feed-webpack-plugin/__tests__/index.js b/src/loader-feed-webpack-plugin/__tests__/index.js index 0ce435c50..c7a13df50 100644 --- a/src/loader-feed-webpack-plugin/__tests__/index.js +++ b/src/loader-feed-webpack-plugin/__tests__/index.js @@ -15,6 +15,7 @@ rimraf(outputPath) it("Feed webpack plugin", () => { return new Promise((resolve, reject) => webpack( { + context: __dirname, module: { [webpackVersion() === 1 ? "loaders" : "rules"]: [ { diff --git a/src/loader/__tests__/index.js b/src/loader/__tests__/index.js index 63365b189..9435f1bfe 100644 --- a/src/loader/__tests__/index.js +++ b/src/loader/__tests__/index.js @@ -184,16 +184,18 @@ it("can be used with plugins", () => { console.log(stats.compilation.warnings) } + let expectations = 0 Object.keys(stats.compilation.assets) .filter((key) => key.endsWith(".json")) .forEach((key) => { const result = JSON.parse(stats.compilation.assets[key]._value) if (result.test) { expect(result.test).toBe("dumb") + expectations++ } }) - // t.plan(2+5) // 2, err, warn, 5 => array - // t.end() + // check we got all the expectation we wanted + expect(expectations).toBe(5) }, (err) => expect(err).toBe(undefined) ) diff --git a/src/redux/modules/__tests__/pages.js b/src/redux/modules/__tests__/pages.js index 430824f90..36dcf7f16 100644 --- a/src/redux/modules/__tests__/pages.js +++ b/src/redux/modules/__tests__/pages.js @@ -6,9 +6,10 @@ import reducer, * as module from "../pages" process.env.PHENOMIC_USER_PATHNAME = "/" test("should have action to handle get", async (t) => { - // t.plan(3) + let expectations = 0 global.fetch = (url) => { + expectations++ t.is(url, "/url") return new Promise((resolve) => resolve( @@ -19,15 +20,17 @@ test("should have action to handle get", async (t) => { )) } const action = module.get("/page", "/url") + expect(expectations).toBe(1) t.is(action.page, "/page") t.deepEqual(action.types, [ module.GET, module.SET, module.ERROR ]) }) test("should have action to handle refresh", async (t) => { - // t.plan(3) + let expectations = 0 global.fetch = (url) => { + expectations++ t.is(url, "/url") return new Promise((resolve) => resolve( @@ -39,6 +42,7 @@ test("should have action to handle refresh", async (t) => { } const action = module.refresh("/page", "/url") + expect(expectations).toBe(1) t.is(action.page, "/page") t.deepEqual(action.types, [ module.NOOP, module.SET, module.ERROR ]) }) diff --git a/src/static/routes-to-urls/__tests__/index.js b/src/static/routes-to-urls/__tests__/index.js index 768f1ba95..18b91a36d 100644 --- a/src/static/routes-to-urls/__tests__/index.js +++ b/src/static/routes-to-urls/__tests__/index.js @@ -93,14 +93,12 @@ const routesNoMatches = ( ) -it("log message if there is no matches", () => { - jest.resetModules() - - const log = jest.fn() - routesToUrls(routesNoMatches, collection, { log }) - - expect(log).toBeCalled() - expect(log.mock.calls[0][0]).toContain( - "It looks like some parameters can't be mapped to create routes: :lol" - ) +it("log message if there is no matches", (done) => { + routesToUrls(routesNoMatches, collection, (log) => { + if (log.includes( + "It looks like some parameters can't be mapped to create routes: :lol" + )) { + done() + } + }) }) diff --git a/src/static/routes-to-urls/index.js b/src/static/routes-to-urls/index.js index 8e8ba58cb..685619239 100644 --- a/src/static/routes-to-urls/index.js +++ b/src/static/routes-to-urls/index.js @@ -5,7 +5,7 @@ import colors from "chalk" import arrayUnique from "../../_utils/array-unique" -const defaultConsole = console +const defaultConsole = console.log const flattenRoute = (route) => { const root = route.path ? route.path : "" @@ -77,7 +77,7 @@ const paramsListFromCollection = (collection) => { return params } -const createUrlsFromParamsReplacementInUrl = (url, params, console) => { +const createUrlsFromParamsReplacementInUrl = (url, params, log) => { // don't compute anything if url doesn't seems to have dynamic parameters // react-router url params are like ``:that`` (or splat *) if (url.indexOf(":") === -1 && url.indexOf("*") === -1) { @@ -100,7 +100,7 @@ const createUrlsFromParamsReplacementInUrl = (url, params, console) => { nonMissingKeys.push(paramName) } catch (e) { - // console.log(paramName, e.message) + // log(paramName, e.message) const matches = e.message.match(/Missing \"(.*)\" parameter for path/) if (matches && matches[1]) { @@ -123,7 +123,7 @@ const createUrlsFromParamsReplacementInUrl = (url, params, console) => { (key) => nonMissingKeys.indexOf(key) === -1 ) if (missingKeys.length) { - console.log("⚠️ " + colors.red( + log("⚠️ " + colors.red( "It looks like some parameters can't be mapped to create routes: ", missingKeys.map((key) => ":" + key).join(", "), )) @@ -135,13 +135,13 @@ const createUrlsFromParamsReplacementInUrl = (url, params, console) => { return arrayUnique(urls.sort()) } -const hydrateRoutesUrls = (routesUrls, collection, console) => { +const hydrateRoutesUrls = (routesUrls, collection, log) => { const paramsList = paramsListFromCollection(collection) return routesUrls.reduce((acc, url) => { return [ ...acc, - ...createUrlsFromParamsReplacementInUrl(url, paramsList, console), + ...createUrlsFromParamsReplacementInUrl(url, paramsList, log), ] }, []) } @@ -149,9 +149,9 @@ const hydrateRoutesUrls = (routesUrls, collection, console) => { export default ( routes: React$Element, collection: PhenomicCollection, - console: typeof console = defaultConsole, + // for testing + log: Function = defaultConsole, ): Array => { - const flattenedRoutes = createRoutes(routes) .reduce((acc, r) => [ ...acc, ...flattenRoute(r) ], []) @@ -164,5 +164,5 @@ export default ( ) } - return hydrateRoutesUrls(flattenedRoutes, collection, console) + return hydrateRoutesUrls(flattenedRoutes, collection, log) } diff --git a/src/static/to-html/__tests__/index.js b/src/static/to-html/__tests__/index.js index 98b9d94a9..5f7eef302 100644 --- a/src/static/to-html/__tests__/index.js +++ b/src/static/to-html/__tests__/index.js @@ -31,8 +31,6 @@ test("don't break if there is nothing to transform", async (t) => { }) test("writeAllHTMLFiles", (t) => { - // t.plan(3) - return writeAllHTMLFiles({ routes: require("./fixtures/routes.js").default, collection, @@ -49,7 +47,6 @@ test("writeAllHTMLFiles", (t) => { if (!expectedHTML[filename]) { throw new Error(filename + " is missing in expectedHTML results") } - t.deepEqual( beautifyHTML(html), expectedHTML[filename](), diff --git a/themes/phenomic-theme-base/package.json b/themes/phenomic-theme-base/package.json index 87d69ed21..1cdf1339a 100644 --- a/themes/phenomic-theme-base/package.json +++ b/themes/phenomic-theme-base/package.json @@ -17,6 +17,13 @@ "#babel": "webpack-(development|production) are useful for webpack 2, otherwise use development|production", "babel": { "env": { + "test": { + "presets": [ + "babel-preset-latest", + "babel-preset-stage-1", + "babel-preset-react" + ] + }, "development": { "presets": [ "babel-preset-latest",