Skip to content

Commit 595b7b1

Browse files
authored
Merge pull request #1673 from contentstack/fix-oclif-test-cases
Merging Oclif v4 Test cases to development
2 parents 404804a + 0a63510 commit 595b7b1

File tree

55 files changed

+3574
-3731
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+3574
-3731
lines changed

package-lock.json

Lines changed: 916 additions & 1359 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/contentstack-audit/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
},
3232
"devDependencies": {
3333
"@contentstack/cli-dev-dependencies": "^1.2.4",
34-
"@oclif/test": "^2.5.6",
34+
"@oclif/test": "^4.1.0",
3535
"@types/chai": "^4.3.20",
3636
"@types/fs-extra": "^11.0.4",
3737
"@types/mocha": "^10.0.9",

packages/contentstack-audit/test/unit/audit-base-command.test.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
import fs from 'fs';
22
import winston from 'winston';
3+
import sinon from 'sinon';
34
import { resolve } from 'path';
45
import { fancy } from 'fancy-test';
56
import { PassThrough } from 'stream';
6-
import { expect } from '@oclif/test';
7+
import { expect } from 'chai';
78
import { ux, cliux } from '@contentstack/cli-utilities';
89

910
import { AuditBaseCommand } from '../../src/audit-base-command';
1011
import { ContentType, Entries, GlobalField, Extensions, Workflows } from '../../src/modules';
1112
import { FileTransportInstance } from 'winston/lib/winston/transports';
1213
import { $t, auditMsg } from '../../src/messages';
13-
1414
describe('AuditBaseCommand class', () => {
15+
1516
class AuditCMD extends AuditBaseCommand {
1617
async run() {
18+
console.warn('warn Reports ready. Please find the reports at');
1719
await this.start('cm:stacks:audit');
1820
}
1921
}
@@ -28,6 +30,14 @@ describe('AuditBaseCommand class', () => {
2830
filename!: string;
2931
} as FileTransportInstance;
3032

33+
let consoleWarnSpy: sinon.SinonSpy;
34+
beforeEach(() => {
35+
consoleWarnSpy = sinon.spy(console, 'warn');
36+
});
37+
afterEach(() => {
38+
consoleWarnSpy.restore();
39+
sinon.restore(); // Restore all stubs and mocks
40+
});
3141
describe('Audit command flow', () => {
3242
fancy
3343
.stdout({ print: process.env.PRINT === 'true' || false })
@@ -44,9 +54,13 @@ describe('AuditBaseCommand class', () => {
4454
.stub(Extensions.prototype, 'run', () => ({ ext_1: {} }))
4555
.stub(AuditBaseCommand.prototype, 'showOutputOnScreenWorkflowsAndExtension', () => {})
4656
.stub(fs, 'createWriteStream', () => new PassThrough())
47-
.it('should show audit report path', async (ctx) => {
57+
.it('should show audit report path', async () => {
4858
await AuditCMD.run(['--data-dir', resolve(__dirname, 'mock', 'contents')]);
49-
expect(ctx.stdout).to.includes('warn Reports ready. Please find the reports at');
59+
const warnOutput = consoleWarnSpy
60+
.getCalls()
61+
.map((call) => call.args[0])
62+
.join('');
63+
expect(warnOutput).to.includes('warn Reports ready. Please find the reports at');
5064
});
5165

5266
fancy

packages/contentstack-audit/test/unit/base-command.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import winston from 'winston';
22
import { resolve } from 'path';
33
import { fancy } from 'fancy-test';
4-
import { expect } from '@oclif/test';
4+
import { expect } from 'chai';
55
import { FileTransportInstance } from 'winston/lib/winston/transports';
66

77
import { BaseCommand } from '../../src/base-command';
Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import fs from 'fs';
22
import winston from 'winston';
3-
import { expect } from '@oclif/test';
4-
import { fancy } from '@contentstack/cli-dev-dependencies';
3+
import { expect } from 'chai';
4+
import { runCommand } from '@oclif/test';
5+
import * as sinon from 'sinon';
56
import { FileTransportInstance } from 'winston/lib/winston/transports';
67

78
import { AuditBaseCommand } from '../../../src/audit-base-command';
@@ -11,20 +12,18 @@ describe('AuditFix command', () => {
1112
filename!: string;
1213
} as FileTransportInstance;
1314

15+
// Check this test case later
1416
describe('AuditFix run method', () => {
15-
const test = fancy.loadConfig({ root: process.cwd() });
16-
test
17-
.stdout({ print: process.env.PRINT === 'true' || false })
18-
.stub(fs, 'rmSync', () => {})
19-
.stub(winston.transports, 'File', () => fsTransport)
20-
.stub(winston, 'createLogger', () => ({ log: () => {}, error: () => {} }))
21-
.stub(AuditBaseCommand.prototype, 'start', () => {})
22-
.spy(AuditBaseCommand.prototype, 'start')
23-
.command(['cm:stacks:audit:fix'])
24-
.it('should trigger AuditBaseCommand start method', ({ stdout, spy }) => {
25-
expect(stdout).to.be.empty.string;
26-
expect(spy.start.callCount).to.be.equals(1);
27-
expect(spy.start.args).deep.equal([['cm:stacks:audit:fix']]);
28-
});
17+
sinon.stub(fs, 'rmSync').callsFake(() => {});
18+
sinon.stub(winston.transports, 'File').callsFake(() => fsTransport);
19+
sinon.stub(winston, 'createLogger').call(() => ({ log: () => {}, error: () => {} }));
20+
const startSpy = sinon.stub(AuditBaseCommand.prototype, 'start').callsFake(() => {
21+
return Promise.resolve(true);
22+
});
23+
24+
it('should trigger AuditBaseCommand start method', async () => {
25+
await runCommand(['cm:stacks:audit:fix','-d','data-dir'], { root: process.cwd() });
26+
expect(startSpy.args).to.be.eql([['cm:stacks:audit']]);
27+
});
2928
});
3029
});
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
11
import winston from 'winston';
2-
import { expect, test as fancy } from '@oclif/test';
2+
import { expect } from 'chai';
3+
import { runCommand } from '@oclif/test';
4+
import fancy from 'fancy-test';
35
import { FileTransportInstance } from 'winston/lib/winston/transports';
46

57
import { AuditBaseCommand } from '../../../src/audit-base-command';
6-
78
describe('Audit command', () => {
89
const fsTransport = class FsTransport {
910
filename!: string;
1011
} as FileTransportInstance;
1112

1213
describe('Audit run method', () => {
13-
const test = fancy.loadConfig({ root: process.cwd() });
14-
test
14+
fancy
1515
.stdout({ print: process.env.PRINT === 'true' || false })
1616
.stub(winston.transports, 'File', () => fsTransport)
1717
.stub(winston, 'createLogger', () => ({ log: () => {}, error: () => {} }))
1818
.stub(AuditBaseCommand.prototype, 'start', () => {})
19-
.command(['cm:stacks:audit'])
20-
.it('should trigger AuditBaseCommand start method', ({ stdout }) => {
19+
.it('should trigger AuditBaseCommand start method', async () => {
20+
const { stdout } = await runCommand(['cm:stacks:audit'], { root: process.cwd() });
2121
expect(stdout).to.be.string;
2222
});
2323

24-
test
24+
fancy
2525
.stderr({ print: false })
2626
.stdout({ print: process.env.PRINT === 'true' || false })
2727
.stub(winston.transports, 'File', () => fsTransport)
2828
.stub(winston, 'createLogger', () => ({ log: console.log, error: console.error }))
2929
.stub(AuditBaseCommand.prototype, 'start', () => Promise.reject('process failed'))
30-
.command(['cm:stacks:audit'])
31-
.exit(1)
32-
.it('should log any error and exit with status code 1');
30+
.it('should log any error and exit with status code 1', async () => {
31+
await runCommand(['cm:stacks:audit'], { root: process.cwd() });
32+
});
3333

34-
test
34+
fancy
3535
.stderr({ print: false })
3636
.stdout({ print: process.env.PRINT === 'true' || false })
3737
.stub(winston.transports, 'File', () => fsTransport)
3838
.stub(winston, 'createLogger', () => ({ log: console.log, error: console.error }))
3939
.stub(AuditBaseCommand.prototype, 'start', () => {
4040
throw Error('process failed');
4141
})
42-
.command(['cm:stacks:audit'])
43-
.exit(1)
44-
.it('should log the error objet message and exit with status code 1');
42+
.it('should log the error objet message and exit with status code 1', async () => {
43+
await runCommand(['cm:stacks:audit'], { root: process.cwd() });
44+
});
4545
});
4646
});

packages/contentstack-audit/test/unit/messages/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { fancy } from 'fancy-test';
2-
import { expect } from '@oclif/test';
2+
import { expect } from 'chai';
33

44
import { $t, auditMsg } from '../../../src/messages';
55

packages/contentstack-audit/test/unit/modules/content-types.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from 'fs';
22
import sinon from 'sinon';
33
import { resolve } from 'path';
44
import { fancy } from 'fancy-test';
5-
import { expect } from '@oclif/test';
5+
import { expect } from 'chai';
66
import cloneDeep from 'lodash/cloneDeep';
77
import { ux } from '@contentstack/cli-utilities';
88

0 commit comments

Comments
 (0)