From 41c8e23c71b53ff2127ca6b8acf2acabb32a5e16 Mon Sep 17 00:00:00 2001 From: Kyle Pollich Date: Wed, 10 Jan 2018 11:35:17 -0500 Subject: [PATCH 1/2] Don't generate pages for test files --- .../__tests__/gatsby-node.js | 22 +++++++++++++++++++ .../component-page-creator/validate-path.js | 8 ++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/packages/gatsby/src/internal-plugins/component-page-creator/__tests__/gatsby-node.js b/packages/gatsby/src/internal-plugins/component-page-creator/__tests__/gatsby-node.js index e06da2fff6673..c1cb9c1ab24ba 100644 --- a/packages/gatsby/src/internal-plugins/component-page-creator/__tests__/gatsby-node.js +++ b/packages/gatsby/src/internal-plugins/component-page-creator/__tests__/gatsby-node.js @@ -53,6 +53,28 @@ describe(`JavaScript page creator`, () => { expect(files.filter(file => validatePath(file.path)).length).toEqual(2) }) + it(`filters out test files`, () => { + const files = [ + { + path: `__tests__/something.test.js`, + }, + { + path: `foo.spec.js`, + }, + { + path: `bar.test.js`, + }, + { + path: `page.js`, + }, + { + path: `page.jsx`, + }, + ] + + expect(files.filter(file => validatePath(file.path)).length).toEqual(2) + }) + describe(`create-path`, () => { it(`should create unix paths`, () => { const basePath = `/a/` diff --git a/packages/gatsby/src/internal-plugins/component-page-creator/validate-path.js b/packages/gatsby/src/internal-plugins/component-page-creator/validate-path.js index 89f70ed54a004..c9543ac919ad5 100644 --- a/packages/gatsby/src/internal-plugins/component-page-creator/validate-path.js +++ b/packages/gatsby/src/internal-plugins/component-page-creator/validate-path.js @@ -2,6 +2,11 @@ const systemPath = require(`path`) const tsDeclarationExtTest = /\.d\.tsx?$/ +function isTestFile(path) { + const testFileTest = new RegExp(`(/__tests__/.*|(\\.|/)(test|spec))\\.jsx?$`) + return path.match(testFileTest) +} + module.exports = path => { // Disallow paths starting with an underscore // and template-. @@ -10,6 +15,7 @@ module.exports = path => { return ( parsedPath.name.slice(0, 1) !== `_` && parsedPath.name.slice(0, 9) !== `template-` && - !tsDeclarationExtTest.test(parsedPath.base) + !tsDeclarationExtTest.test(parsedPath.base) && + !isTestFile(path) ) } From 52912a53c22c1563496e19fcfe35eeca9e6e92fe Mon Sep 17 00:00:00 2001 From: Kyle Pollich Date: Wed, 10 Jan 2018 12:25:34 -0500 Subject: [PATCH 2/2] Use parsedPath.base instead of path --- .../internal-plugins/component-page-creator/validate-path.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/gatsby/src/internal-plugins/component-page-creator/validate-path.js b/packages/gatsby/src/internal-plugins/component-page-creator/validate-path.js index c9543ac919ad5..7f00afc1646d4 100644 --- a/packages/gatsby/src/internal-plugins/component-page-creator/validate-path.js +++ b/packages/gatsby/src/internal-plugins/component-page-creator/validate-path.js @@ -16,6 +16,6 @@ module.exports = path => { parsedPath.name.slice(0, 1) !== `_` && parsedPath.name.slice(0, 9) !== `template-` && !tsDeclarationExtTest.test(parsedPath.base) && - !isTestFile(path) + !isTestFile(parsedPath.base) ) }