-
Notifications
You must be signed in to change notification settings - Fork 402
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(lib): add unit tests for
PackageManagerFactory.prototype.find
- Loading branch information
1 parent
ea9cdf5
commit f37bde8
Showing
1 changed file
with
64 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import * as fs from 'fs'; | ||
import { | ||
NpmPackageManager, | ||
PackageManagerFactory, | ||
PnpmPackageManager, | ||
YarnPackageManager, | ||
} from '../../../lib/package-managers'; | ||
|
||
jest.mock('fs', () => ({ | ||
promises: { | ||
readdir: jest.fn(), | ||
}, | ||
})); | ||
|
||
describe('PackageManagerFactory', () => { | ||
afterAll(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('.prototype.find()', () => { | ||
it('should return NpmPackageManager when no lock file is found', async () => { | ||
(fs.promises.readdir as jest.Mock).mockResolvedValue([]); | ||
|
||
const whenPackageManager = PackageManagerFactory.find(); | ||
await expect(whenPackageManager).resolves.toBeInstanceOf( | ||
NpmPackageManager, | ||
); | ||
}); | ||
|
||
it('should return YarnPackageManager when "yarn.lock" file is found', async () => { | ||
(fs.promises.readdir as jest.Mock).mockResolvedValue(['yarn.lock']); | ||
|
||
const whenPackageManager = PackageManagerFactory.find(); | ||
await expect(whenPackageManager).resolves.toBeInstanceOf( | ||
YarnPackageManager, | ||
); | ||
}); | ||
|
||
it('should return PnpmPackageManager when "pnpm-lock.yaml" file is found', async () => { | ||
(fs.promises.readdir as jest.Mock).mockResolvedValue(['pnpm-lock.yaml']); | ||
|
||
const whenPackageManager = PackageManagerFactory.find(); | ||
await expect(whenPackageManager).resolves.toBeInstanceOf( | ||
PnpmPackageManager, | ||
); | ||
}); | ||
|
||
describe('when there are all supported lock files', () => { | ||
it('should prioritize "yarn.lock" file over all the others lock files', async () => { | ||
(fs.promises.readdir as jest.Mock).mockResolvedValue([ | ||
'pnpm-lock.yaml', | ||
'package-lock.json', | ||
// This is intentionally the last element in this array | ||
'yarn.lock', | ||
]); | ||
|
||
const whenPackageManager = PackageManagerFactory.find(); | ||
await expect(whenPackageManager).resolves.toBeInstanceOf( | ||
YarnPackageManager, | ||
); | ||
}); | ||
}); | ||
}); | ||
}); |