Skip to content

Commit

Permalink
fix(gatsby-plugin-page-creator): ensure that __tests__ directory is a…
Browse files Browse the repository at this point in the history
…ctually ignored (#9720)

* fix: ensure that __tests__ directory is actually ignored

This (simple) PR fixes a bug I was noticing in #9629, specifically that
__tests__/*.js files _were_ being included in the bundle.

This is because the isTestFile function in gatsby-plugin-page-creator is
passed the file name, not the file name _with_ the folder (__tests__ in
this case).

I added a second check to check the dir, but could also just implement
this check with the raw file path, as well

* chore: use back ticks

* fix: account for nested directories and switch to micromatch

* chore: split to test windows path matching

* chore: run linter

* chore: add windows path matching test (micromatch supports natively)

* fix: pass single string to isMatch, instead of array

* Revert "fix: pass single string to isMatch, instead of array"

This reverts commit dbc2ffe.
  • Loading branch information
DSchau authored and pieh committed Nov 6, 2018
1 parent 88a671a commit a6d17d8
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 47 deletions.
4 changes: 2 additions & 2 deletions packages/gatsby-plugin-page-creator/package.json
Expand Up @@ -24,6 +24,7 @@
"fs-exists-cached": "^1.0.0",
"glob": "^7.1.1",
"lodash": "^4.17.10",
"micromatch": "^3.1.10",
"parse-filepath": "^1.0.1",
"slash": "^1.0.0"
},
Expand All @@ -35,6 +36,5 @@
},
"peerDependencies": {
"gatsby": ">2.0.0-alpha"
},
"gitHead": "5bd5aebe066b9875354a81a4b9ed98722731c465"
}
}
66 changes: 36 additions & 30 deletions packages/gatsby-plugin-page-creator/src/__tests__/gatsby-node.js
Expand Up @@ -10,28 +10,26 @@ describe(`JavaScript page creator`, () => {
{ path: `somedir/dir2/test1.js` },
]

expect(validFiles.filter(file => validatePath(file.path)).length).toEqual(validFiles.length)
expect(validFiles.filter(file => validatePath(file.path))).toEqual(
validFiles
)
})

it(`filters out files that start with underscores`, () => {
const validFiles = [
{ path: `something/blah.js` },
{ path: `test1.js` },
]
const validFiles = [{ path: `something/blah.js` }, { path: `test1.js` }]

const testFiles = validFiles.concat([
{ path: `something/_foo.js` },
{ path: `_blah.js` },
])

expect(testFiles.filter(file => validatePath(file.path)).length).toEqual(validFiles.length)
expect(testFiles.filter(file => validatePath(file.path))).toEqual(
validFiles
)
})

it(`filters out files that start with dot`, () => {
const validFiles = [
{ path: `something/blah.js` },
{ path: `test1.ts` },
]
const validFiles = [{ path: `something/blah.js` }, { path: `test1.ts` }]

const testFiles = validFiles.concat([
{ path: `.eslintrc` },
Expand All @@ -41,14 +39,13 @@ describe(`JavaScript page creator`, () => {
{ path: `something/.markdownlint.json` },
])

expect(testFiles.filter(file => validatePath(file.path)).length).toEqual(validFiles.length)
expect(testFiles.filter(file => validatePath(file.path))).toEqual(
validFiles
)
})

it(`filters out json and yaml files`, () => {
const validFiles = [
{ path: `somefile.js` },
{ path: `something/blah.js` },
]
const validFiles = [{ path: `somefile.js` }, { path: `something/blah.js` }]

const testFiles = validFiles.concat([
{ path: `something/otherConfig.yml` },
Expand All @@ -58,20 +55,21 @@ describe(`JavaScript page creator`, () => {
{ path: `dir1/dir2/file.json` },
])

expect(testFiles.filter(file => validatePath(file.path)).length).toEqual(validFiles.length)
expect(testFiles.filter(file => validatePath(file.path))).toEqual(
validFiles
)
})

it(`filters out files that start with template-*`, () => {
const validFiles = [
{ path: `something/blah.js` },
{ path: `file1.js` },
]
const validFiles = [{ path: `something/blah.js` }, { path: `file1.js` }]

const testFiles = validFiles.concat([
{ path: `template-cool-page-type.js` },
])

expect(testFiles.filter(file => validatePath(file.path)).length).toEqual(validFiles.length)
expect(testFiles.filter(file => validatePath(file.path))).toEqual(
validFiles
)
})

it(`filters out files that have TypeScript declaration extensions`, () => {
Expand All @@ -85,22 +83,30 @@ describe(`JavaScript page creator`, () => {
{ path: `something-else/other-declaration-file.d.tsx` },
])

expect(testFiles.filter(file => validatePath(file.path)).length).toEqual(validFiles.length)
expect(testFiles.filter(file => validatePath(file.path))).toEqual(
validFiles
)
})

it(`filters out test files`, () => {
const validFiles = [
{ path: `page.js` },
{ path: `page.jsx` },
]
const validFiles = [{ path: `page.js` }, { path: `page.jsx` }]

const testFiles = validFiles.concat([
{ path: `__tests__/something.test.js` },
{ path: `foo.spec.js` },
{ path: `bar.test.js` },
{ path: `src/pages/__tests__/something.test.js` },
{ path: `src/pages/__tests__/something.test.tsx` },
{ path: `src/pages/__tests__/something.js` },
{ path: `src/pages/__tests__/nested-directory/something.js` },
{ path: `src/pages/foo.spec.js` },
{ path: `src/pages/foo.spec.tsx` },
{ path: `src/pages/bar.test.js` },
{ path: `src/pages/bar.test.tsx` },
{ path: `src\\pages\\bar.test.js` },
{ path: `src\\pages\\__tests__\\nested-directory\\something.js` },
])

expect(testFiles.filter(file => validatePath(file.path)).length).toEqual(validFiles.length)
expect(testFiles.filter(file => validatePath(file.path))).toEqual(
validFiles
)
})

describe(`create-path`, () => {
Expand Down
14 changes: 10 additions & 4 deletions packages/gatsby-plugin-page-creator/src/validate-path.js
@@ -1,11 +1,17 @@
const systemPath = require(`path`)
const mm = require(`micromatch`)

const tsDeclarationExtTest = /\.d\.tsx?$/
const jsonYamlExtTest = /\.(json|ya?ml)$/

function isTestFile(path) {
const testFileTest = new RegExp(`(/__tests__/.*|(\\.|/)(test|spec))\\.jsx?$`)
return path.match(testFileTest)
// https://github.com/facebook/jest/blob/v24.0.0-alpha.4/packages/jest-config/src/Defaults.js#L71
function isTestFile(filePath) {
const testPatterns = [
`**/__tests__/**/*.(js|ts)?(x)`,
`**/?(*.)+(spec|test).(js|ts)?(x)`,
]

return mm.isMatch(filePath, testPatterns)
}

module.exports = path => {
Expand All @@ -19,6 +25,6 @@ module.exports = path => {
parsedPath.name.slice(0, 9) !== `template-` &&
!tsDeclarationExtTest.test(parsedPath.base) &&
!jsonYamlExtTest.test(parsedPath.base) &&
!isTestFile(parsedPath.base)
!isTestFile(path)
)
}
11 changes: 0 additions & 11 deletions yarn.lock
Expand Up @@ -3504,17 +3504,6 @@ babel-preset-flow@^6.23.0:
dependencies:
babel-plugin-transform-flow-strip-types "^6.22.0"

babel-preset-gatsby-package@^0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/babel-preset-gatsby-package/-/babel-preset-gatsby-package-0.1.2.tgz#418d52343bea31e1594ec69d106207fafa5ffaf7"
dependencies:
"@babel/plugin-proposal-class-properties" "^7.0.0"
"@babel/plugin-proposal-optional-chaining" "^7.0.0"
"@babel/plugin-transform-runtime" "^7.0.0"
"@babel/preset-env" "^7.0.0"
"@babel/preset-flow" "^7.0.0"
"@babel/preset-react" "^7.0.0"

babel-preset-jest@^22.4.4:
version "22.4.4"
resolved "https://registry.yarnpkg.com/babel-preset-jest/-/babel-preset-jest-22.4.4.tgz#ec9fbd8bcd7dfd24b8b5320e0e688013235b7c39"
Expand Down

0 comments on commit a6d17d8

Please sign in to comment.