Skip to content

Commit 13a2da8

Browse files
author
Alexey Litvinov
committed
test for environment
1 parent 57249c7 commit 13a2da8

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

lib/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ module.exports = function setupHook({
108108

109109
tokens = lazyResult.root.tokens;
110110

111+
if (process.env.NODE_ENV !== 'development') {
112+
// updating cache
113+
tokensByFile[filename] = tokens;
114+
} else {
115+
// clearing cache in development mode
116+
delete require.cache[filename];
117+
}
118+
111119
if (processCss) {
112120
processCss(lazyResult.css, filename);
113121
}

test/env/fixture/oceanic.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.color
2+
{
3+
background: #1e2a35;
4+
}

test/env/node_env.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const detachHook = require('../sugar').detachHook;
2+
const dropCache = require('../sugar').dropCache;
3+
const resolve = require('path').resolve;
4+
const writeFileSync = require('fs').writeFileSync;
5+
6+
const destination = resolve(__dirname, './fixture/oceanic.css');
7+
const source1 = `.color\n{\n background: #1e2a35;\n}\n`;
8+
const source2 = `.awesome-color\n{\n background: #1e2a35;\n}\n`;
9+
10+
suite('env.NODE_ENV', () => {
11+
suite('in the development mode', () => {
12+
test('should get tokens from fs, not from cache', () => {
13+
const tokens = require('./fixture/oceanic.css');
14+
15+
assert.deepEqual(tokens, {
16+
'awesome-color': '_test_env_fixture_oceanic__awesome-color',
17+
});
18+
});
19+
20+
setup(() => {
21+
process.env.NODE_ENV = 'development';
22+
hook({});
23+
writeFile(source1);
24+
require('./fixture/oceanic.css');
25+
writeFile(source2);
26+
});
27+
28+
teardown(() => {
29+
process.env.NODE_ENV = '';
30+
writeFile(source1);
31+
detachHook('.css');
32+
dropCache('./env/fixture/oceanic.css');
33+
});
34+
});
35+
36+
suite('not in the development mode', () => {
37+
test('should get tokens from cache', () => {
38+
const tokens = require('./fixture/oceanic.css');
39+
40+
assert.deepEqual(tokens, {
41+
'color': '_test_env_fixture_oceanic__color',
42+
});
43+
});
44+
45+
setup(() => {
46+
hook({});
47+
require('./fixture/oceanic.css');
48+
});
49+
50+
teardown(() => {
51+
detachHook('.css');
52+
dropCache('./env/fixture/oceanic.css');
53+
});
54+
});
55+
});
56+
57+
/**
58+
* @param {string} data
59+
*/
60+
function writeFile(data) {
61+
writeFileSync(destination, data, 'utf8');
62+
}

0 commit comments

Comments
 (0)