Skip to content

Commit c447f2f

Browse files
committed
chore: Cleanup shared test utils package.
- Move @css-blocks/build => @css-blocks/test-utils. - Move @css-blocks/test-utils to devDependencies in JSX integration. - Clean up exports API of test-utils so we don't need deep requires. - Clean up typings of mock function in test-utils so we don't need Object.apply. - Add README for test-utils. - Rename transient-fs to mock-fs. - Move _files and _directories private properties to truly private module scoped contants.
1 parent 41bdd29 commit c447f2f

30 files changed

+80
-46
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ language: node_js
77
node_js:
88
- "8"
99
- "10"
10+
- "11"
1011

1112
env:
1213
- CXX=g++-4.8

css-blocks.code-workspace

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"path": "packages/@css-blocks/ember-cli"
3636
},
3737
{
38-
"path": "packages/@css-blocks/build"
38+
"path": "packages/@css-blocks/test-utils"
3939
}
4040
],
4141
"settings": {

packages/@css-blocks/build/README.md

-3
This file was deleted.

packages/@css-blocks/build/src/index.ts

-1
This file was deleted.

packages/@css-blocks/build/src/testing/index.ts

-1
This file was deleted.

packages/@css-blocks/core/src/Analyzer/ElementAnalysis.ts

+1
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,7 @@ function mapChoiceClasses(
851851
configuration: ResolvedConfiguration,
852852
map: Map<string, Style>,
853853
includeAbsent: boolean,
854+
/* tslint:disable-next-line */
854855
...styles: Style[]
855856
): AttributeValueChoice {
856857
let choices = new Array<AttributeValueChoiceOption>();

packages/@css-blocks/jsx/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"access": "public"
4141
},
4242
"devDependencies": {
43+
"@css-blocks/test-utils": "^0.20.0-beta.4",
4344
"@css-blocks/code-style": "^0.20.0-beta.4",
4445
"@css-blocks/runtime": "^0.20.0-beta.4",
4546
"babel-core": "^6.25.0",
@@ -50,7 +51,6 @@
5051
"@css-blocks/runtime": "^0.18.0"
5152
},
5253
"dependencies": {
53-
"@css-blocks/build": "^0.20.0-beta.4",
5454
"@css-blocks/core": "^0.20.0-beta.4",
5555
"@opticss/template-api": "^0.4.0",
5656
"@opticss/util": "^0.4.0",

packages/@css-blocks/jsx/test/analyzer/class-states-objstr-test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import mock from "@css-blocks/build/dist/src/testing/transient-fs";
1+
import { mock } from "@css-blocks/test-utils";
2+
23
import { assert } from "chai";
34
import { suite, test } from "mocha-typescript";
45

packages/@css-blocks/jsx/test/analyzer/dynamic-styles-test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import mock from "@css-blocks/build/dist/src/testing/transient-fs";
1+
import { mock } from "@css-blocks/test-utils";
22

33
import { assert } from "chai";
44
import { suite, test } from "mocha-typescript";

packages/@css-blocks/jsx/test/analyzer/external-objstr-class-test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import mock from "@css-blocks/build/dist/src/testing/transient-fs";
1+
import { mock } from "@css-blocks/test-utils";
22

33
import { assert } from "chai";
44
import { suite, test } from "mocha-typescript";

packages/@css-blocks/jsx/test/analyzer/external-objstr-root-styles-test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import mock from "@css-blocks/build/dist/src/testing/transient-fs";
1+
import { mock } from "@css-blocks/test-utils";
2+
23
import { assert } from "chai";
34
import { suite, test } from "mocha-typescript";
45

packages/@css-blocks/jsx/test/analyzer/inline-class-styles-test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import mock from "@css-blocks/build/dist/src/testing/transient-fs";
1+
import { mock } from "@css-blocks/test-utils";
2+
23
import { assert } from "chai";
34
import { suite, test } from "mocha-typescript";
45

packages/@css-blocks/jsx/test/analyzer/inline-objstr-styles-test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import mock from "@css-blocks/build/dist/src/testing/transient-fs";
1+
import { mock } from "@css-blocks/test-utils";
2+
23
import { assert } from "chai";
34
import { suite, test } from "mocha-typescript";
45

packages/@css-blocks/jsx/test/analyzer/inline-root-styles-test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import mock from "@css-blocks/build/dist/src/testing/transient-fs";
1+
import { mock } from "@css-blocks/test-utils";
2+
23
import { assert } from "chai";
34
import { suite, test } from "mocha-typescript";
45

packages/@css-blocks/jsx/test/analyzer/root-states-objstr-test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import mock from "@css-blocks/build/dist/src/testing/transient-fs";
1+
import { mock } from "@css-blocks/test-utils";
2+
23
import { assert } from "chai";
34
import { suite, test } from "mocha-typescript";
45

packages/@css-blocks/jsx/test/analyzer/states-attributes-test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import mock from "@css-blocks/build/dist/src/testing/transient-fs";
1+
import { mock } from "@css-blocks/test-utils";
2+
23
import { assert } from "chai";
34
import { skip, suite, test } from "mocha-typescript";
45

packages/@css-blocks/jsx/test/analyzer/typed-file-analysis.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import mock from "@css-blocks/build/dist/src/testing/transient-fs";
1+
import { mock } from "@css-blocks/test-utils";
2+
23
import { assert } from "chai";
34
import { skip, suite, test } from "mocha-typescript";
45

packages/@css-blocks/jsx/test/blockImporter-test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import mock from "@css-blocks/build/dist/src/testing/transient-fs";
1+
import { mock } from "@css-blocks/test-utils";
2+
23
import { Block } from "@css-blocks/core";
34
import { assert } from "chai";
45
import { suite, test } from "mocha-typescript";

packages/@css-blocks/jsx/test/parser-test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import mock from "@css-blocks/build/dist/src/testing/transient-fs";
1+
import { mock } from "@css-blocks/test-utils";
2+
23
import { assert } from "chai";
34
import { suite, test } from "mocha-typescript";
45

packages/@css-blocks/jsx/test/transformer/transformer-test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import mock from "@css-blocks/build/dist/src/testing/transient-fs";
1+
import { mock } from "@css-blocks/test-utils";
2+
23
import { Analysis, BlockCompiler, Options as CSSBlocksOptions, StyleMapping, resolveConfiguration as resolveBlocksConfiguration } from "@css-blocks/core";
34
import c$$ from "@css-blocks/runtime";
45
import { TemplateIntegrationOptions } from "@opticss/template-api";
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Shared Test Utilities for `@css-blocks` Packages
2+
3+
These utilities are only used in the @css-blocks tests, so this package is not published.
4+
5+
## `import { mock } from @css-blocks/test-utils`
6+
7+
The `mock` utility provides tests the ability to temporarily mocks files on the filesystem. Works for all supported versions of Node.js. Once mocked, files generated using `mock` can be accessed like regular files by all Node.js filesystem calls.
8+
9+
You can mock files using the following API:
10+
11+
```js
12+
mock({
13+
'filename.js': 'file-contents',
14+
'foldernames': {
15+
'can': {
16+
'nest': {
17+
'file.css': 'file-contents'
18+
}
19+
}
20+
}
21+
});
22+
```
23+
24+
To clean up all mocked filesystem files and directories when done, simply call:
25+
26+
```js
27+
mock.restore();
28+
```

packages/@css-blocks/build/package.json renamed to packages/@css-blocks/test-utils/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
2-
"name": "@css-blocks/build",
2+
"name": "@css-blocks/test-utils",
33
"private": true,
44
"version": "0.20.0-beta.4",
5-
"description": "Build-time support for @css-blocks/*.",
6-
"main": "dist/src/index.js",
5+
"description": "Shared test utilities for @css-blocks/*.",
6+
"main": "dist/src",
77
"scripts": {
88
"compile": "tsc --version && rm -rf dist && tsc",
99
"pretest": "yarn run compile",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./mock-fs";

packages/@css-blocks/build/src/testing/transient-fs.ts renamed to packages/@css-blocks/test-utils/src/mock-fs/index.ts

+17-20
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ export interface FileContent {
66
[filename: string]: string | FileContent;
77
}
88

9-
export interface TransientFiles {
9+
export interface FileMocker {
1010
(filesObj: FileContent): void;
1111
restore: () => void;
12-
_files: string[];
13-
_directories: string[];
12+
fileCount: () => number;
1413
}
1514

16-
function transientFS(filesObj: FileContent) {
15+
const FILES: string[] = [];
16+
const DIRS: string[] = [];
17+
18+
export const mock = <FileMocker>function mock(filesObj: FileContent) {
1719
let keys = Object.keys(filesObj);
1820
for (let key of keys) {
1921
let content = filesObj[key];
@@ -23,41 +25,36 @@ function transientFS(filesObj: FileContent) {
2325
if (!existsSync(key)) {
2426
mkdirSync(key);
2527
let fullPath = path.resolve(key);
26-
transient._directories.push(fullPath);
28+
DIRS.push(fullPath);
2729
}
2830
process.chdir(key);
29-
transientFS(content);
31+
mock(content);
3032
}
3133
finally {
3234
process.chdir(dir);
3335
}
3436
} else {
3537
let fullPath = path.resolve(key);
3638
writeFileSync(fullPath, content);
37-
transient._files.push(fullPath);
39+
FILES.push(fullPath);
3840
}
3941
}
40-
}
42+
};
4143

42-
function restore() {
44+
mock.restore = function restore() {
4345
let file, dir;
44-
while (file = transient._files.pop()) {
46+
while (file = FILES.pop()) {
4547
if (existsSync(file)) {
4648
unlinkSync(file);
4749
}
4850
}
49-
while (dir = transient._directories.pop()) {
51+
while (dir = DIRS.pop()) {
5052
if (existsSync(dir)) {
5153
rmdirSync(dir);
5254
}
5355
}
54-
}
55-
56-
const transient: TransientFiles = Object.assign(transientFS, {
57-
_files: [],
58-
_directories: [],
59-
restore,
60-
});
56+
};
6157

62-
// tslint:disable-next-line no-default-export
63-
export default transient;
58+
mock.fileCount = function fileCount(): number {
59+
return FILES.length;
60+
};

packages/@css-blocks/build/test/transient-fs-test.ts renamed to packages/@css-blocks/test-utils/test/mock-fs-test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import * as fs from "fs";
33
import { suite, test } from "mocha-typescript";
44
import * as path from "path";
55

6-
import mock from "../src/testing/transient-fs";
6+
import { mock } from "../src/mock-fs";
77

8-
@suite("transient-fs")
8+
@suite("mock-fs")
99
export class TransientFsTest {
1010
@test "can write and clean up files"() {
1111
mock({
@@ -40,7 +40,7 @@ export class TransientFsTest {
4040
assert.isFalse(fs.existsSync("top-file.txt"));
4141
}
4242
@test "can restore no files"() {
43-
assert.equal(mock._files.length, 0);
43+
assert.equal(mock.fileCount(), 0);
4444
mock.restore();
4545
}
4646
}

0 commit comments

Comments
 (0)