Skip to content

Commit

Permalink
fix: add unit tests for yarn.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
dmeents committed Nov 27, 2022
1 parent ee03189 commit 28b5ecd
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 5 deletions.
7 changes: 4 additions & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const { jest } = require("@dmeents/maestro");
const { jest } = require('@dmeents/maestro');

module.exports = {
...jest({
packageName: "semantic-release-yarn",
tsconfig: "./tsconfig.json",
packageName: 'semantic-release-yarn',
isNode: true,
tsconfig: 'tsconfig.json',
}),
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"jest-junit": "^15.0.0",
"prettier": "^2.8.0",
"semantic-release": "^19.0.5",
"tempy": "^1.0.1",
"ts-jest": "^29.0.3",
"typescript": "^4.9.3"
},
Expand Down
3 changes: 2 additions & 1 deletion renovate.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"ignoreDeps": [
"execa",
"read-pkg"
"read-pkg",
"tempy"
],
"extends": [
":autodetectPinVersions",
Expand Down
94 changes: 94 additions & 0 deletions src/utils/yarn.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import * as fs from 'fs';
import { Yarn } from './yarn';
import tempy from 'tempy';

const HOME = tempy.directory();
const YARN_RC_FILENAME = 'yarnrc.test.yaml';
const yarn = new Yarn({ HOME, YARN_RC_FILENAME });

describe('Yarn', () => {
it('setNpmRegistryServer', async () => {
await yarn.setNpmRegistryServer('https://registry.npmjs.org');
expect(await yarn.getNpmRegistryServer()).toBe(
'https://registry.npmjs.org',
);
});

it('setNpmAuthToken', async () => {
const token = '__dummy_token__';
await yarn.setNpmAuthToken(token);
expect(await yarn.getNpmAuthToken()).toBe(token);
});

it('with real NPM_TOKEN', async () => {
const NPM_TOKEN = process.env['NPM_TOKEN'] as string;
expect(NPM_TOKEN).toBeDefined();

await yarn.setNpmAuthToken(NPM_TOKEN);
expect(await yarn.authenticated()).toBe(true);
});

it('invalid NPM_TOKEN', async () => {
await yarn.setNpmAuthToken('invalid');
expect(await yarn.authenticated()).toBe(false);
});

it('version', async () => {
const cwd = tempy.directory();
const packageJson = `${cwd}/package.json`;

fs.writeFileSync(
packageJson,
JSON.stringify({ version: '1.0.0', packageManager: 'yarn@3.1.0' }),
);

function getVersion(): string {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
return JSON.parse(fs.readFileSync(packageJson, 'utf8')).version as string;
}

const yarn = new Yarn({ HOME, YARN_RC_FILENAME, cwd });
await yarn.install();
await yarn.pluginImportVersion();
await yarn.version('1.0.1');
expect(getVersion()).toBe('1.0.1');
});

it('yarnPackDryRun', async () => {
const cwd = tempy.directory();
const packageJson = `${cwd}/package.json`;
fs.writeFileSync(
packageJson,
JSON.stringify({ version: '1.0.0', packageManager: 'yarn@3.1.0' }),
);

fs.mkdirSync(`${cwd}/lib`);
fs.writeFileSync(`${cwd}/lib/index.js`, '');
const yarn = new Yarn({ HOME, YARN_RC_FILENAME, cwd });
const files = await yarn.packDryRun();
expect(files).toMatchInlineSnapshot(`
[
"lib/index.js",
"package.json",
]
`);
});

it('yarnPack', async () => {
const cwd = tempy.directory();
const packageJson = `${cwd}/package.json`;

fs.writeFileSync(
packageJson,
JSON.stringify({
name: 'mypkg',
version: '1.0.0',
packageManager: 'yarn@3.1.0',
}),
);

const yarn = new Yarn({ HOME, YARN_RC_FILENAME, cwd });
await yarn.pack('%s-%v.tgz');
expect(fs.readdirSync(cwd)).toContain('mypkg-1.0.0.tgz');
});
});
3 changes: 2 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ __metadata:
read-pkg: ^5.2.0
semantic-release: ^19.0.5
semver: ^7.3.8
tempy: ^1.0.1
ts-jest: ^29.0.3
typescript: ^4.9.3
languageName: unknown
Expand Down Expand Up @@ -6952,7 +6953,7 @@ __metadata:
languageName: node
linkType: hard

"tempy@npm:^1.0.0":
"tempy@npm:^1.0.0, tempy@npm:^1.0.1":
version: 1.0.1
resolution: "tempy@npm:1.0.1"
dependencies:
Expand Down

0 comments on commit 28b5ecd

Please sign in to comment.