Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for running from command line #492

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions fixtures/missing-translations/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "missing-translations"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When running the bin/cli script from a fixture, the let rootDir = pkgDir.sync(); line would return the root of this whole repo, because there is no package.json in fixtures. Adding it with just a name fixed this.

}
3 changes: 3 additions & 0 deletions fixtures/no-issues/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "no-issues"
}
3 changes: 3 additions & 0 deletions fixtures/remove-unused-translations/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "remove-unused-translations"
}
3 changes: 3 additions & 0 deletions fixtures/unused-translations/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "unused-translations"
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"eslint-config-prettier": "8.5.0",
"eslint-plugin-node": "11.1.0",
"eslint-plugin-prettier": "4.0.0",
"execa": "^5.0.0",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This dependency actually came with Jest already, hence no changes in yarn.lock. Also note that version 6.x doesn't work here, because of the same reason that requireing it doesn't work then.

"jest": "27.5.1",
"lerna-changelog": "2.2.0",
"prettier": "2.6.2",
Expand Down
39 changes: 39 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const execa = require('execa');
const fs = require('fs');
const { run, generateFileList } = require('./index');

Expand Down Expand Up @@ -59,3 +60,41 @@ describe('generateFileList', () => {
expect(() => generateFileList([])).toThrow('Unexpected empty file list');
});
});

describe('Running from cli', () => {
test('without unused translations', async () => {
let { stdout } = await execa('node', ['../../bin/cli'], {
cwd: `${__dirname}/fixtures/no-issues`,
});

expect(stdout).toMatch('No unused translations');
});

test('with unused translations', async () => {
expect(
execa('node', ['../../bin/cli'], { cwd: `${__dirname}/fixtures/unused-translations` })
).rejects.toThrowError('Found 2 unused translations');
});

test('with missing translations', async () => {
expect(
execa('node', ['../../bin/cli'], { cwd: `${__dirname}/fixtures/missing-translations` })
).rejects.toThrowError('Found 2 missing translations');
});

describe('with auto-fix', () => {
afterEach(async function () {
await execa('git', ['checkout', 'HEAD', 'fixtures/remove-unused-translations/translations'], {
cwd: __dirname,
});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The auto-fix option actually changes the files, so this makes sure those changes are undone. It does feel a bit specific though, so any other ideas are welcome :)

});

test('with unused translations', async () => {
let { stdout } = await execa('node', ['../../bin/cli', '--fix'], {
cwd: `${__dirname}/fixtures/remove-unused-translations`,
});

expect(stdout).toMatch('All unused translations were removed');
});
});
});