Skip to content
This repository has been archived by the owner on Jan 6, 2021. It is now read-only.

Commit

Permalink
test(JSON): add JSON value tests that work on both Windows and UNIX (#…
Browse files Browse the repository at this point in the history
…112)

add working tests that use the same JSON value on both Windows and UNIX

Closes #30
  • Loading branch information
Apidcloud authored and Kent C. Dodds committed May 22, 2017
1 parent 9c4f346 commit c9908f3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md 100644 → 100755
Expand Up @@ -85,6 +85,17 @@ the parent. This is quite useful for launching the same command with different
env variables or when the environment variables are too long to have everything
in one line.

Lastly, if you want to pass a JSON string (e.g., when using [ts-loader]), you can do as follows:
```json
{
"scripts": {
"test": "cross-env TS_NODE_COMPILER_OPTIONS={\\\"module\\\":\\\"commonjs\\\"} node some_file.test.ts"
}
}
```
Pay special attention to the **triple backslash** `(\\\)` **before** the **double quotes** `(")` and the **absence** of **single quotes** `(')`.
Both of these conditions have to be met in order to work both on Windows and UNIX.

## `cross-env` vs `cross-env-shell`

The `cross-env` module exposes two bins: `cross-env` and `cross-env-shell`. The
Expand Down Expand Up @@ -179,3 +190,4 @@ MIT
[win-bash]: https://msdn.microsoft.com/en-us/commandline/wsl/about
[angular-formly]: https://github.com/formly-js/angular-formly
[cross-spawn]: https://www.npmjs.com/package/cross-spawn
[ts-loader]: https://www.npmjs.com/package/ts-loader
14 changes: 14 additions & 0 deletions src/variable.test.js
@@ -1,15 +1,19 @@
import isWindowsMock from 'is-windows'
import varValueConvert from './variable'

const JSON_VALUE = '{\\"foo\\":\\"bar\\"}'

beforeEach(() => {
process.env.VAR1 = 'value1'
process.env.VAR2 = 'value2'
process.env.JSON_VAR = JSON_VALUE
isWindowsMock.__mock.reset()
})

afterEach(() => {
delete process.env.VAR1
delete process.env.VAR2
delete process.env.JSON_VAR
})

test(`doesn't affect simple variable values`, () => {
Expand Down Expand Up @@ -82,3 +86,13 @@ test(`resolves an env variable value for non-existant variable`, () => {
isWindowsMock.__mock.returnValue = true
expect(varValueConvert('foo-$VAR_POTATO')).toBe('foo-')
})

test(`resolves an env variable with a JSON string value on Windows`, () => {
isWindowsMock.__mock.returnValue = true
expect(varValueConvert('$JSON_VAR')).toBe(JSON_VALUE)
})

test(`resolves an env variable with a JSON string value on UNIX`, () => {
isWindowsMock.__mock.returnValue = false
expect(varValueConvert('$JSON_VAR')).toBe(JSON_VALUE)
})

0 comments on commit c9908f3

Please sign in to comment.