|
| 1 | +import { OutputMode } from "@css-blocks/core"; |
| 2 | +import assert = require("assert"); |
| 3 | +import path = require("path"); |
| 4 | +import { chdir, cwd } from "process"; |
| 5 | + |
| 6 | +import { load, search } from "../src"; |
| 7 | + |
| 8 | +function fixture(...relativePathSegments: Array<string>): string { |
| 9 | + return path.resolve(__dirname, "..", "..", "test", "fixtures", ...relativePathSegments); |
| 10 | +} |
| 11 | + |
| 12 | +const WORKING_DIR = cwd(); |
| 13 | + |
| 14 | +describe("validate", () => { |
| 15 | + afterEach(() => { |
| 16 | + chdir(WORKING_DIR); |
| 17 | + }); |
| 18 | + it("can load configuration from the package.json in the current working directory", async () => { |
| 19 | + chdir(fixture("from-pkg-json")); |
| 20 | + let options = await search(); |
| 21 | + if (options === null) { |
| 22 | + assert.fail("configuration wasn't found."); |
| 23 | + } else { |
| 24 | + assert.equal(options.outputMode, OutputMode.BEM); |
| 25 | + } |
| 26 | + }); |
| 27 | + it("can load configuration from the package.json in a specified directory", async () => { |
| 28 | + let options = await search(fixture("from-pkg-json")); |
| 29 | + if (options === null) { |
| 30 | + assert.fail("configuration wasn't found."); |
| 31 | + } else { |
| 32 | + assert.equal(options.outputMode, OutputMode.BEM); |
| 33 | + } |
| 34 | + }); |
| 35 | + it("will load configuration from the first package.json in ancestor directory", async () => { |
| 36 | + let options = await search(fixture("from-pkg-json", "subdir", "another-subdir")); |
| 37 | + if (options === null) { |
| 38 | + assert.fail("configuration wasn't found."); |
| 39 | + } else { |
| 40 | + assert.equal(options.outputMode, OutputMode.BEM); |
| 41 | + } |
| 42 | + }); |
| 43 | + it("loads preprocessors if a file is specified.", async () => { |
| 44 | + let options = await search(fixture("from-pkg-json")); |
| 45 | + if (options === null) { |
| 46 | + assert.fail("configuration wasn't found."); |
| 47 | + } else { |
| 48 | + assert.equal(options.preprocessors && typeof options.preprocessors.scss, "function"); |
| 49 | + } |
| 50 | + }); |
| 51 | + it("can load configuration from css-blocks.config.json in the current working directory", async () => { |
| 52 | + chdir(fixture("from-json-file")); |
| 53 | + let options = await search(); |
| 54 | + if (options === null) { |
| 55 | + assert.fail("configuration wasn't found."); |
| 56 | + } else { |
| 57 | + assert.equal(options.outputMode, OutputMode.BEM_UNIQUE); |
| 58 | + } |
| 59 | + }); |
| 60 | + it("can load configuration from css-blocks.config.json in a specified directory", async () => { |
| 61 | + let options = await search(fixture("from-json-file")); |
| 62 | + if (options === null) { |
| 63 | + assert.fail("configuration wasn't found."); |
| 64 | + } else { |
| 65 | + assert.equal(options.outputMode, OutputMode.BEM_UNIQUE); |
| 66 | + } |
| 67 | + }); |
| 68 | + it("will load configuration from css-blocks.config.json in an ancestor directory", async () => { |
| 69 | + let options = await search(fixture("from-json-file", "subdir", "another-subdir")); |
| 70 | + if (options === null) { |
| 71 | + assert.fail("configuration wasn't found."); |
| 72 | + } else { |
| 73 | + assert.equal(options.outputMode, OutputMode.BEM_UNIQUE); |
| 74 | + assert.equal(options.rootDir, fixture("from-json-file")); |
| 75 | + } |
| 76 | + }); |
| 77 | + it("can extend configuration from another location", async () => { |
| 78 | + let options = await search(fixture("from-json-file")); |
| 79 | + if (options === null) { |
| 80 | + assert.fail("configuration wasn't found."); |
| 81 | + } else { |
| 82 | + assert.equal(options.preprocessors && typeof options.preprocessors.scss, "function"); |
| 83 | + assert.equal(options.preprocessors && typeof options.preprocessors.less, "function"); |
| 84 | + } |
| 85 | + }); |
| 86 | + it("can specify a file containing an importer", async () => { |
| 87 | + let options = await search(fixture("from-json-file")); |
| 88 | + if (options === null) { |
| 89 | + assert.fail("configuration wasn't found."); |
| 90 | + } else { |
| 91 | + assert.equal(options.importer && typeof options.importer, "object"); |
| 92 | + assert.equal(options.importerData && Array.isArray(options.importerData), true); |
| 93 | + } |
| 94 | + }); |
| 95 | + it("can load configuration from css-blocks.config.js in the current working directory", async () => { |
| 96 | + chdir(fixture("from-js-file")); |
| 97 | + let options = await search(); |
| 98 | + if (options === null) { |
| 99 | + assert.fail("configuration wasn't found."); |
| 100 | + } else { |
| 101 | + assert.equal(options.outputMode, OutputMode.BEM); |
| 102 | + assert.equal(options.maxConcurrentCompiles, 8); |
| 103 | + assert.equal(options.rootDir, fixture("from-js-file", "blocks")); |
| 104 | + assert.equal(options.preprocessors && typeof options.preprocessors.scss, "function"); |
| 105 | + assert.equal(options.preprocessors && typeof options.preprocessors.styl, "function"); |
| 106 | + } |
| 107 | + }); |
| 108 | + it("rootDir is not overridden if specified explicitly", async () => { |
| 109 | + chdir(fixture("another-js-file")); |
| 110 | + let options = await search(); |
| 111 | + if (options === null) { |
| 112 | + assert.fail("configuration wasn't found."); |
| 113 | + } else { |
| 114 | + assert.equal(options.outputMode, OutputMode.BEM); |
| 115 | + assert.equal(options.maxConcurrentCompiles, 8); |
| 116 | + assert.equal(options.rootDir, fixture("from-js-file", "blocks")); |
| 117 | + assert.equal(options.preprocessors && typeof options.preprocessors.scss, "function"); |
| 118 | + assert.equal(options.preprocessors && typeof options.preprocessors.styl, "function"); |
| 119 | + } |
| 120 | + }); |
| 121 | + it("can load a config file from an explicit path.", async () => { |
| 122 | + let options = await load(fixture("another-js-file", "css-blocks.config.js")); |
| 123 | + if (options === null) { |
| 124 | + assert.fail("configuration wasn't found."); |
| 125 | + } else { |
| 126 | + assert.equal(options.outputMode, OutputMode.BEM); |
| 127 | + assert.equal(options.maxConcurrentCompiles, 8); |
| 128 | + assert.equal(options.rootDir, fixture("from-js-file", "blocks")); |
| 129 | + assert.equal(options.preprocessors && typeof options.preprocessors.scss, "function"); |
| 130 | + assert.equal(options.preprocessors && typeof options.preprocessors.styl, "function"); |
| 131 | + } |
| 132 | + }); |
| 133 | +}); |
0 commit comments