Skip to content

Commit

Permalink
test(e2e): add removing .babelrc case
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh committed Apr 2, 2021
1 parent 6f6c981 commit 1076b1f
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,30 @@ describe(`babelrc`, () => {
.invoke(`text`)
.should(`eq`, `babel-rc-added`)
})

it(`removing .babelrc`, () => {
cy.visit(`/babelrc/remove/`).waitForRouteChange()

cy.getTestElement(TEST_ELEMENT)
.invoke(`text`)
.should(`eq`, `babel-rc-will-be-deleted`)

cy.exec(
`npm run update -- --file src/pages/babelrc/remove/.babelrc --delete`
)

// babel-loader doesn't actually hot reloads itself when .babelrc file is deleted
// so to test hot-reloading here we actually have to invalidate js file, which would recompile it and discover
// new babelrc file
cy.exec(
`npm run update -- --file src/pages/babelrc/remove/index.js --replacements "foo-bar:foo-bar" --exact`
)

cy.waitForHmr()

cy.getTestElement(TEST_ELEMENT)
.invoke(`text`)
.should(`eq`, `babel-rc-test`)
})
})
})
26 changes: 18 additions & 8 deletions e2e-tests/development-runtime/scripts/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ const args = yargs
default: false,
type: `boolean`,
})
.option(`delete`, {
default: false,
type: `boolean`,
})
.option(`fileContent`, {
default: JSON.stringify(
`
Expand Down Expand Up @@ -72,15 +76,21 @@ async function update() {
history.set(filePath, exists ? file : false)
}

const contents = replacements.reduce((replaced, pair) => {
const [key, value] = pair.split(`:`)
return replaced.replace(
args.exact ? key : new RegExp(`%${key}%`, `g`),
value
)
}, file)
if (args.delete) {
if (exists) {
await fs.remove(filePath)
}
} else {
const contents = replacements.reduce((replaced, pair) => {
const [key, value] = pair.split(`:`)
return replaced.replace(
args.exact ? key : new RegExp(`%${key}%`, `g`),
value
)
}, file)

await fs.writeFile(filePath, contents, `utf8`)
await fs.writeFile(filePath, contents, `utf8`)
}

await writeHistory(history)
}
Expand Down
20 changes: 20 additions & 0 deletions e2e-tests/development-runtime/src/pages/babelrc/remove/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"plugins": [
[
"babel-plugin-search-and-replace",
{
"rules": [
{
"search": "babel-rc-test",
"replace": "babel-rc-will-be-deleted",
"searchTemplateStrings": true
}

]
}
]
],
"presets": [
"babel-preset-gatsby"
]
}
13 changes: 13 additions & 0 deletions e2e-tests/development-runtime/src/pages/babelrc/remove/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from "react"

export default function BabelrcRemove() {
return (
<>
<p>
Code block below should contain <code>babel-rc-will-be-deleted</code>{" "}
first and after removal it should contain <code>babel-rc-test</code>
</p>
<pre data-testid="test-element">babel-rc-test</pre>
</>
)
}

0 comments on commit 1076b1f

Please sign in to comment.