Created
August 1, 2018 19:34
-
-
Save joshcox/ab670db4d719dede4d308de44b4fbc52 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @ts-ignore @types/memfs does not exist | |
import {createFsFromVolume, Volume} from "memfs"; | |
import path from "path"; | |
// Create a mocked filesystem instance | |
const mockFs = createFsFromVolume(Volume.fromJSON({})); | |
jest.mock("fs", () => mockFs); | |
// Import modules that should be using the mocked filesystem | |
import {proteus} from "@proteus/core"; | |
import {ProjectStructure} from "../src"; | |
interface IDir { [name: string]: IDir | string; } | |
const jsonToMockFS = (cwd: string, dir: IDir): void => Object.keys(dir) | |
.map((name: string): [string, IDir | string] => ([name, dir[name]])) | |
.forEach(([name, value]: [string, IDir | string]) => { | |
const p = path.resolve(cwd, name); | |
if (typeof value === "string") { | |
mockFs.writeFileSync(p, value); | |
} else { | |
mockFs.mkdirpSync(p); | |
jsonToMockFS(p, value); | |
} | |
}); | |
describe("ProjectStructurePlugin", () => { | |
describe(".npmrc", () => { | |
beforeAll(() => { | |
jsonToMockFS("/", { | |
"npmrc-test": { | |
"artifact1": {}, | |
"artifact2": { | |
".npmrc": "mock content 1" | |
} | |
} | |
}); | |
return proteus({ | |
workingDirectory: "/npmrc-test", | |
plugins: [ProjectStructure] | |
}); | |
}); | |
it("does not overwrite existing .npmrc files", () => { | |
expect(mockFs.readFileSync(path.resolve("/npmrc-test/artifact2", ".npmrc")).toString()) | |
.toBe("mock content 1"); | |
}); | |
it("creates a new npmrc when one doesn't exist", () => { | |
expect(mockFs.readFileSync(path.resolve("/npmrc-test/artifact1", ".npmrc")).toString()) | |
.toBe("registry=https://cernerrepos.net/api/npm/npm\npackage-lock=false\n"); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment