|
| 1 | +import fs from 'fs'; |
| 2 | +import tmp from 'tmp'; |
| 3 | +import { promisify } from 'util'; |
| 4 | +import resolveConfig, { reInitializePackageJson } from '../resolve-config'; |
| 5 | + |
| 6 | +const mkdirAsync = promisify(fs.mkdir); |
| 7 | +const writeFileAsync = promisify(fs.writeFile); |
| 8 | + |
| 9 | +const outerPackageJson = { |
| 10 | + config: { |
| 11 | + mongodbMemoryServer: { |
| 12 | + version: '3.0.0', |
| 13 | + }, |
| 14 | + }, |
| 15 | +}; |
| 16 | +const innerPackageJson = { |
| 17 | + config: { |
| 18 | + mongodbMemoryServer: { |
| 19 | + version: '4.0.0', |
| 20 | + }, |
| 21 | + }, |
| 22 | +}; |
| 23 | + |
| 24 | +describe('resolveConfig', () => { |
| 25 | + const originalDir = process.cwd(); |
| 26 | + let tmpObj; |
| 27 | + |
| 28 | + describe('configuration from closest package.json', () => { |
| 29 | + beforeAll(async () => { |
| 30 | + // Set up test project/subproject structure in a temporary directory: |
| 31 | + // |
| 32 | + // project/ |
| 33 | + // |-- subproject/ |
| 34 | + // | +-- package.json |
| 35 | + // +-- package.json |
| 36 | + |
| 37 | + tmpObj = tmp.dirSync({ unsafeCleanup: true }); |
| 38 | + const tmpName = tmpObj.name; |
| 39 | + |
| 40 | + await mkdirAsync(`${tmpName}/project`); |
| 41 | + await mkdirAsync(`${tmpName}/project/subproject`); |
| 42 | + |
| 43 | + // prettier-ignore |
| 44 | + await Promise.all([ |
| 45 | + writeFileAsync( |
| 46 | + `${tmpName}/project/package.json`, |
| 47 | + JSON.stringify(outerPackageJson) |
| 48 | + ), |
| 49 | + writeFileAsync( |
| 50 | + `${tmpName}/project/subproject/package.json`, |
| 51 | + JSON.stringify(innerPackageJson) |
| 52 | + ), |
| 53 | + ]); |
| 54 | + }); |
| 55 | + |
| 56 | + afterAll(() => { |
| 57 | + process.chdir(originalDir); |
| 58 | + tmpObj.removeCallback(); |
| 59 | + }); |
| 60 | + |
| 61 | + test('in project', () => { |
| 62 | + process.chdir(`${tmpObj.name}/project`); |
| 63 | + reInitializePackageJson(); |
| 64 | + const got = resolveConfig('VERSION'); |
| 65 | + expect(got).toBe('3.0.0'); |
| 66 | + }); |
| 67 | + |
| 68 | + test('in subproject', () => { |
| 69 | + process.chdir(`${tmpObj.name}/project/subproject`); |
| 70 | + reInitializePackageJson(); |
| 71 | + const got = resolveConfig('VERSION'); |
| 72 | + expect(got).toBe('4.0.0'); |
| 73 | + }); |
| 74 | + }); |
| 75 | +}); |
0 commit comments