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

refactor: rewrite tests to use webpack compiler, drop node 8 #360

Merged
merged 25 commits into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
start converting old tests
  • Loading branch information
beeequeue committed Mar 4, 2021
commit b058cd392237428202a28c57e5534d7bdc41506b
130 changes: 118 additions & 12 deletions test/compiler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,135 @@ const getConfig = (target, plugin) => ({
plugins: [plugin]
})

const compile = (config, callback) => {
webpack(config, (err, stats) => {
expect(err).toBeNull()
expect(stats.compilation.errors).toHaveLength(0)

const result = readFileSync(
resolve(__dirname, `./output/${config.target}/main.js`),
{ encoding: 'utf-8' }
)

callback(result)
})
}

const expectResultsToContainReplacements = (result, env) => {
Object.entries(env).forEach(([key, value]) => {
expect(result).toMatch(`const ${key} = "${value}"`)
})
}

const versions = [
['Source', Src.default],
['Dist', Dist.default]
]

beforeEach(() => {
rmdirSync(resolve(__dirname, 'output'), { recursive: true })
})

describe.each(versions)('%s', (_, DotenvPlugin) => {
test('Should be an function.', () => {
expect(typeof DotenvPlugin).toEqual('function')
})

test('Should return a instance of Dotenv.', () => {
expect((new DotenvPlugin()).constructor.name).toEqual('Dotenv')
})

describe('Defaults', () => {
test('Should include environment variables that exist in .env file.', (done) => {
const config = getConfig('web', new DotenvPlugin())

compile(config, (result) => {
expectResultsToContainReplacements(result, { TEST: 'hi' })

done()
})
})

test('Should not expand variables by default', (done) => {
const config = getConfig('web', new DotenvPlugin({ path: envExpanded }))

compile(config, (result) => {
expectResultsToContainReplacements(result, {
NODE_ENV: 'test',
BASIC: 'basic',
BASIC_EXPAND: '$BASIC',
MACHINE: 'machine_env',
MACHINE_EXPAND: '$MACHINE',
UNDEFINED_EXPAND: '$UNDEFINED_ENV_KEY',
// eslint-disable-next-line
ESCAPED_EXPAND: '\\\\$ESCAPED',
MONGOLAB_DATABASE: 'heroku_db',
MONGOLAB_USER: 'username',
MONGOLAB_PASSWORD: 'password',
MONGOLAB_DOMAIN: 'abcd1234.mongolab.com',
MONGOLAB_PORT: '12345',
// eslint-disable-next-line
MONGOLAB_URI: 'mongodb:https://${MONGOLAB_USER}:${MONGOLAB_PASSWORD}@${MONGOLAB_DOMAIN}:${MONGOLAB_PORT}/${MONGOLAB_DATABASE}',
// eslint-disable-next-line
MONGOLAB_USER_RECURSIVELY: '${MONGOLAB_USER}:${MONGOLAB_PASSWORD}',
// eslint-disable-next-line
MONGOLAB_URI_RECURSIVELY: 'mongodb:https://${MONGOLAB_USER_RECURSIVELY}@${MONGOLAB_DOMAIN}:${MONGOLAB_PORT}/${MONGOLAB_DATABASE}',
WITHOUT_CURLY_BRACES_URI: 'mongodb:https://$MONGOLAB_USER:$MONGOLAB_PASSWORD@$MONGOLAB_DOMAIN:$MONGOLAB_PORT/$MONGOLAB_DATABASE',
WITHOUT_CURLY_BRACES_USER_RECURSIVELY: '$MONGOLAB_USER:$MONGOLAB_PASSWORD',
WITHOUT_CURLY_BRACES_URI_RECURSIVELY: 'mongodb:https://$MONGOLAB_USER_RECURSIVELY@$MONGOLAB_DOMAIN:$MONGOLAB_PORT/$MONGOLAB_DATABASE'
})

done()
})
})

test('Should expand variables when configured', (done) => {
const config = getConfig(
'web',
new DotenvPlugin({ path: envExpanded, expand: true })
)

compile(config, (result) => {
expectResultsToContainReplacements(result, {
NODE_ENV: 'test',
BASIC: 'basic',
BASIC_EXPAND: 'basic',
MACHINE: 'machine_env',
MACHINE_EXPAND: 'machine_env',
UNDEFINED_EXPAND: '',
// eslint-disable-next-line
ESCAPED_EXPAND: '\$ESCAPED',
MONGOLAB_DATABASE: 'heroku_db',
MONGOLAB_USER: 'username',
MONGOLAB_PASSWORD: 'password',
MONGOLAB_DOMAIN: 'abcd1234.mongolab.com',
MONGOLAB_PORT: '12345',
MONGOLAB_URI: 'mongodb:https://username:[email protected]:12345/heroku_db',
MONGOLAB_USER_RECURSIVELY: 'username:password',
MONGOLAB_URI_RECURSIVELY: 'mongodb:https://username:[email protected]:12345/heroku_db',
WITHOUT_CURLY_BRACES_URI: 'mongodb:https://username:[email protected]:12345/heroku_db',
WITHOUT_CURLY_BRACES_USER_RECURSIVELY: 'username:password',
WITHOUT_CURLY_BRACES_URI_RECURSIVELY: 'mongodb:https://username:[email protected]:12345/heroku_db'
})

done()
})
})
})

describe('process.env stubbing', () => {
const expectToBeStubbed = (result) => {
expect(result).toMatch('const test = "testing"')
expect(result).toMatch('const test2 = "MISSING_ENV_VAR".TEST2')
expect(result).toMatch('const nodeEnv = "development"')
expect(result).toMatch('const mongolabUser = "MISSING_ENV_VAR".MONGOLAB_USER')
expect(result).toMatch('const TEST = "testing"')
expect(result).toMatch('const TEST2 = "MISSING_ENV_VAR".TEST2')
expect(result).toMatch('const NODE_ENV = "development"')
expect(result).toMatch('const MONGOLAB_USER = "MISSING_ENV_VAR".MONGOLAB_USER')
}

const expectNotToBeStubbed = (result) => {
expect(result).toMatch('const test = "testing"')
expect(result).toMatch('const test2 = process.env.TEST2')
expect(result).toMatch('const nodeEnv = "development"')
expect(result).toMatch('const mongolabUser = process.env.MONGOLAB_USER')
expect(result).toMatch('const TEST = "testing"')
expect(result).toMatch('const TEST2 = process.env.TEST2')
expect(result).toMatch('const NODE_ENV = "development"')
expect(result).toMatch('const MONGOLAB_USER = process.env.MONGOLAB_USER')
}

const plugin = new DotenvPlugin({ path: envSimple })
Expand All @@ -66,10 +176,6 @@ describe.each(versions)('%s', (_, DotenvPlugin) => {
['electron9-main', getConfig('electron9-main', plugin), false]
]

beforeEach(() => {
rmdirSync(resolve(__dirname, 'output'), { recursive: true })
})

test.each(cases)('%s', (target, config, shouldStub, done) => {
webpack(config, (err, stats) => {
expect(err).toBeNull()
Expand Down
33 changes: 23 additions & 10 deletions test/fixtures/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
/* eslint-disable no-unused-vars */
const test = process.env.TEST
const test2 = process.env.TEST2
const nodeEnv = process.env.NODE_ENV
const mongolabUser = process.env.MONGOLAB_USER

export default {
test,
test2,
nodeEnv,
mongolabUser
}
// Basic
const TEST = process.env.TEST
const TEST2 = process.env.TEST2

// Expanded
const NODE_ENV = process.env.NODE_ENV
const BASIC = process.env.BASIC
const BASIC_EXPAND = process.env.BASIC_EXPAND
const MACHINE = process.env.MACHINE
const MACHINE_EXPAND = process.env.MACHINE_EXPAND
const UNDEFINED_EXPAND = process.env.UNDEFINED_EXPAND
const ESCAPED_EXPAND = process.env.ESCAPED_EXPAND
const MONGOLAB_DATABASE = process.env.MONGOLAB_DATABASE
const MONGOLAB_USER = process.env.MONGOLAB_USER
const MONGOLAB_PASSWORD = process.env.MONGOLAB_PASSWORD
const MONGOLAB_DOMAIN = process.env.MONGOLAB_DOMAIN
const MONGOLAB_PORT = process.env.MONGOLAB_PORT
const MONGOLAB_URI = process.env.MONGOLAB_URI
const MONGOLAB_USER_RECURSIVELY = process.env.MONGOLAB_USER_RECURSIVELY
const MONGOLAB_URI_RECURSIVELY = process.env.MONGOLAB_URI_RECURSIVELY
const WITHOUT_CURLY_BRACES_URI = process.env.WITHOUT_CURLY_BRACES_URI
const WITHOUT_CURLY_BRACES_USER_RECURSIVELY = process.env.WITHOUT_CURLY_BRACES_USER_RECURSIVELY
const WITHOUT_CURLY_BRACES_URI_RECURSIVELY = process.env.WITHOUT_CURLY_BRACES_URI_RECURSIVELY