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

fix(gatsby-plugin-page-creator): ensure that __tests__ directory is actually ignored #9720

Merged
merged 8 commits into from
Nov 6, 2018
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
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
4 changes: 2 additions & 2 deletions packages/gatsby-plugin-page-creator/package.json
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also removed all the .length checks. It's a lot safer to validate the actual array shape rather than the length of the array, even though there weren't any changes required here!

e.g. if one of the validFiles was failing and one of the invalid files was passing, we could still get the expected length

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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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