Skip to content

Commit

Permalink
test: added the test for using without babel-plugin-macros.config.js
Browse files Browse the repository at this point in the history
  • Loading branch information
chengjianhua committed Apr 16, 2018
1 parent cd6f7d7 commit de71d5c
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 45 deletions.
2 changes: 1 addition & 1 deletion babel-plugin-macros.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
penv: {
targetName: 'NODE_ENV',
targetName: 'APP_ENV',
},
}
65 changes: 60 additions & 5 deletions src/__tests__/__snapshots__/macro.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`macros should remove expression if \`env\` is a standalone expression: should remove expression if \`env\` is a standalone expression 1`] = `
exports[`penv.macro # with config macros should remove expression if \`env\` is a standalone expression: should remove expression if \`env\` is a standalone expression 1`] = `
"
import penv from './src/macro';
penv({
unmatched: () => {},
});
Expand All @@ -14,10 +14,10 @@ penv({
"
`;

exports[`macros should replace the environment variable to the matched value: should replace the environment variable to the matched value 1`] = `
exports[`penv.macro # with config macros should replace the environment variable to the matched value: should replace the environment variable to the matched value 1`] = `
"
import env from './src/macro';
const variable = env({
development: 'development',
staging: 'staging',
Expand All @@ -39,10 +39,65 @@ const variable = () => {
"
`;

exports[`macros should use \`null\` as default value if no relevant value was matched: should use \`null\` as default value if no relevant value was matched 1`] = `
exports[`penv.macro # with config macros should use \`null\` as default value if no relevant value was matched: should use \`null\` as default value if no relevant value was matched 1`] = `
"
import inlineEnv from './src/macro';
const variable = inlineEnv({
development: 'development',
staging: 'staging',
production: 'production',
});
↓ ↓ ↓ ↓ ↓ ↓
const variable = null;
"
`;

exports[`penv.macro # without config macros should remove expression if \`env\` is a standalone expression: should remove expression if \`env\` is a standalone expression 1`] = `
"
import penv from './src/macro';
penv({
unmatched: () => {},
});
↓ ↓ ↓ ↓ ↓ ↓
"
`;

exports[`penv.macro # without config macros should replace the environment variable to the matched value: should replace the environment variable to the matched value 1`] = `
"
import env from './src/macro';
const variable = env({
development: 'development',
staging: 'staging',
production: 'production',
test: () => {
console.log('test');
return 'test';
}
});
↓ ↓ ↓ ↓ ↓ ↓
const variable = () => {
console.log('test');
return 'test';
};
"
`;

exports[`penv.macro # without config macros should use \`null\` as default value if no relevant value was matched: should use \`null\` as default value if no relevant value was matched 1`] = `
"
import inlineEnv from './src/macro';
const variable = inlineEnv({
development: 'development',
staging: 'staging',
Expand Down
118 changes: 79 additions & 39 deletions src/__tests__/macro.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,88 @@ import plugin from 'babel-plugin-macros'

process.env.NODE_ENV = 'test'

pluginTester({
plugin,
snapshot: true,
tests: {
'should replace the environment variable to the matched value': {
code: `
import env from './src/macro';
const variable = env({
development: 'development',
staging: 'staging',
production: 'production',
test: () => {
console.log('test');
return 'test';
}
});
`,
},
const testMacro = () => {
pluginTester({
plugin,
snapshot: true,
tests: {
'should replace the environment variable to the matched value': {
code: `
import env from './src/macro';
const variable = env({
development: 'development',
staging: 'staging',
production: 'production',
test: () => {
console.log('test');
return 'test';
}
});
`,
},

'should use `null` as default value if no relevant value was matched': {
code: `
import inlineEnv from './src/macro';
'should use `null` as default value if no relevant value was matched': {
code: `
import inlineEnv from './src/macro';
const variable = inlineEnv({
development: 'development',
staging: 'staging',
production: 'production',
});
`,
},

const variable = inlineEnv({
development: 'development',
staging: 'staging',
production: 'production',
});
`,
'should remove expression if `env` is a standalone expression': {
// only: true,
code: `
import penv from './src/macro';
penv({
unmatched: () => {},
});
`,
},
},
})
}

'should remove expression if `env` is a standalone expression': {
// only: true,
code: `
import penv from './src/macro';
describe('penv.macro', () => {
describe('# with config', () => {
let originalAppEnv

penv({
unmatched: () => {},
});
`,
},
},
beforeAll(() => {
originalAppEnv = process.env.APP_ENV
process.env.APP_ENV = 'test'
})

testMacro()

afterAll(() => {
process.env.APP_ENV = originalAppEnv
})
})

describe('# without config', () => {
let originalNodeEnv

beforeAll(() => {
originalNodeEnv = process.env.NODE_ENV
process.env.NODE_ENV = 'test'

jest.doMock('cosmiconfig', () => {
return () => ({load: () => null})
})
})

testMacro()

afterAll(() => {
jest.resetModules()

process.env.NODE_ENV = originalNodeEnv
})
})
})
26 changes: 26 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1819,6 +1819,10 @@ conventional-changelog@^1.1.0:
conventional-changelog-jshint "^0.3.8"
conventional-changelog-preset-loader "^1.1.8"

conventional-commit-types@^2.0.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/conventional-commit-types/-/conventional-commit-types-2.2.0.tgz#5db95739d6c212acbe7b6f656a11b940baa68946"

conventional-commits-filter@^1.1.1, conventional-commits-filter@^1.1.6:
version "1.1.6"
resolved "https://registry.yarnpkg.com/conventional-commits-filter/-/conventional-commits-filter-1.1.6.tgz#4389cd8e58fe89750c0b5fb58f1d7f0cc8ad3831"
Expand Down Expand Up @@ -1984,6 +1988,16 @@ currently-unhandled@^0.4.1:
dependencies:
array-find-index "^1.0.1"

cz-conventional-changelog@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/cz-conventional-changelog/-/cz-conventional-changelog-2.1.0.tgz#2f4bc7390e3244e4df293e6ba351e4c740a7c764"
dependencies:
conventional-commit-types "^2.0.0"
lodash.map "^4.5.1"
longest "^1.0.1"
right-pad "^1.0.1"
word-wrap "^1.0.3"

d@1:
version "1.0.0"
resolved "https://registry.yarnpkg.com/d/-/d-1.0.0.tgz#754bb5bfe55451da69a58b94d45f4c5b0462d58f"
Expand Down Expand Up @@ -4565,6 +4579,10 @@ lodash.isarray@^3.0.0:
version "3.0.4"
resolved "https://registry.yarnpkg.com/lodash.isarray/-/lodash.isarray-3.0.4.tgz#79e4eb88c36a8122af86f844aa9bcd851b5fbb55"

lodash.map@^4.5.1:
version "4.6.0"
resolved "https://registry.yarnpkg.com/lodash.map/-/lodash.map-4.6.0.tgz#771ec7839e3473d9c4cde28b19394c3562f4f6d3"

lodash.merge@^4.6.0:
version "4.6.1"
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.1.tgz#adc25d9cb99b9391c59624f379fbba60d7111d54"
Expand Down Expand Up @@ -5913,6 +5931,10 @@ right-align@^0.1.1:
dependencies:
align-text "^0.1.1"

right-pad@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/right-pad/-/right-pad-1.0.1.tgz#8ca08c2cbb5b55e74dafa96bf7fd1a27d568c8d0"

rimraf@2, rimraf@^2.2.8, rimraf@^2.5.1, rimraf@^2.5.4, rimraf@^2.6.1, rimraf@^2.6.2:
version "2.6.2"
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.6.2.tgz#2ed8150d24a16ea8651e6d6ef0f47c4158ce7a36"
Expand Down Expand Up @@ -7008,6 +7030,10 @@ [email protected]:
version "0.1.0"
resolved "https://registry.yarnpkg.com/window-size/-/window-size-0.1.0.tgz#5438cd2ea93b202efa3a19fe8887aee7c94f9c9d"

word-wrap@^1.0.3:
version "1.2.3"
resolved "https://registry.yarnpkg.com/word-wrap/-/word-wrap-1.2.3.tgz#610636f6b1f703891bd34771ccb17fb93b47079c"

[email protected]:
version "0.0.2"
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-0.0.2.tgz#b79669bb42ecb409f83d583cad52ca17eaa1643f"
Expand Down

0 comments on commit de71d5c

Please sign in to comment.