Skip to content

Commit 654a562

Browse files
update import test cases
1 parent 963d6a6 commit 654a562

4 files changed

Lines changed: 81 additions & 2 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
// Minimal test helper for unit tests
2+
const { createRequire } = require('node:module');
3+
const requireNode = createRequire(__filename);
4+
// Same node:fs object @contentstack/cli-utilities uses; tests stub this so readContentTypeSchemas sees stubs
5+
globalThis.__CONTENTSTACK_TEST_FS__ = requireNode('node:fs');
6+
27
module.exports = {
38
// Basic test utilities can be added here
49
};

packages/contentstack-import/test/unit/import/modules/base-class.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,15 +186,16 @@ describe('BaseClass', () => {
186186
await testClass.delay(0);
187187
const end = Date.now();
188188

189-
expect(end - start).to.be.at.most(10); // Should be very fast
189+
// Wall-clock can exceed a few ms under load (CI); only assert "no intentional wait"
190+
expect(end - start).to.be.at.most(150);
190191
});
191192

192193
it('should handle negative delay', async () => {
193194
const start = Date.now();
194195
await testClass.delay(-100);
195196
const end = Date.now();
196197

197-
expect(end - start).to.be.at.most(10); // Should be very fast
198+
expect(end - start).to.be.at.most(150);
198199
});
199200

200201
it('should return a promise', () => {

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import { expect } from 'chai';
22
import sinon from 'sinon';
3+
import type { PathLike } from 'node:fs';
34
import ImportContentTypes from '../../../../src/import/modules/content-types';
5+
6+
declare global {
7+
// Set in test/helpers/init.js (required first by .mocharc) — must be real require('node:fs'), not import * wrapper
8+
var __CONTENTSTACK_TEST_FS__: typeof import('node:fs');
9+
}
10+
const fs = globalThis.__CONTENTSTACK_TEST_FS__;
411
import { ImportConfig } from '../../../../src/types';
512
import { fsUtil } from '../../../../src/utils';
613
import * as contentTypeHelper from '../../../../src/utils/content-type-helper';
@@ -70,6 +77,36 @@ describe('ImportContentTypes', () => {
7077
return undefined;
7178
});
7279

80+
// readContentTypeSchemas() uses node:fs, not FsUtility — mirror prototype stubs so tests keep working.
81+
// Only intercept content_types paths; fall back to real fs for winston, ts-node, etc.
82+
const originalExistsSync = fs.existsSync.bind(fs);
83+
const originalReaddirSync = fs.readdirSync.bind(fs);
84+
const originalReadFileSync = fs.readFileSync.bind(fs);
85+
sinon.stub(fs, 'existsSync').callsFake((p: PathLike) => {
86+
const s = String(p);
87+
if (s.includes('content_types')) {
88+
return true;
89+
}
90+
return originalExistsSync(p);
91+
});
92+
sinon.stub(fs, 'readdirSync').callsFake((dirPath: PathLike) => {
93+
const s = String(dirPath);
94+
if (s.includes('content_types')) {
95+
return (FsUtility.prototype.readdir as sinon.SinonStub)(s);
96+
}
97+
return originalReaddirSync(dirPath);
98+
});
99+
sinon.stub(fs, 'readFileSync').callsFake((filePath: string | Buffer | URL, encoding?: Parameters<typeof fs.readFileSync>[1]) => {
100+
const p = String(filePath);
101+
if (p.includes('content_types')) {
102+
const r = (FsUtility.prototype.readFile as sinon.SinonStub)(p);
103+
if (r !== undefined) {
104+
return typeof r === 'string' ? r : JSON.stringify(r);
105+
}
106+
}
107+
return originalReadFileSync(filePath, encoding as any);
108+
});
109+
73110
updateFieldRulesStub = sinon.stub(contentTypeHelper, 'updateFieldRules');
74111
lookupExtensionStub = sinon.stub(extensionHelper, 'lookupExtension');
75112
lookUpTaxonomyStub = sinon.stub(taxonomiesHelper, 'lookUpTaxonomy');

packages/contentstack-import/test/unit/import/modules/entries.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { expect } from 'chai';
22
import sinon from 'sinon';
3+
import type { PathLike } from 'node:fs';
34
import EntriesImport from '../../../../src/import/modules/entries';
5+
6+
declare global {
7+
var __CONTENTSTACK_TEST_FS__: typeof import('node:fs');
8+
}
9+
const fs = globalThis.__CONTENTSTACK_TEST_FS__;
410
import { ImportConfig, ModuleClassParams } from '../../../../src/types';
511
import { FsUtility } from '@contentstack/cli-utilities';
612
import { fsUtil, fileHelper, MODULE_CONTEXTS } from '../../../../src/utils';
@@ -36,6 +42,36 @@ describe('EntriesImport', () => {
3642
// This also stubs fsUtil.readFile since fsUtil is an instance of FsUtility
3743
// Don't set a default return value - let individual tests configure it
3844
fsUtilityReadFileStub = sinon.stub(FsUtility.prototype, 'readFile');
45+
46+
// readContentTypeSchemas() uses node:fs; only intercept content_types paths (see content-types.test.ts).
47+
const originalExistsSync = fs.existsSync.bind(fs);
48+
const originalReaddirSync = fs.readdirSync.bind(fs);
49+
const originalReadFileSync = fs.readFileSync.bind(fs);
50+
sinon.stub(fs, 'existsSync').callsFake((p: PathLike) => {
51+
const s = String(p);
52+
if (s.includes('content_types')) {
53+
return true;
54+
}
55+
return originalExistsSync(p);
56+
});
57+
sinon.stub(fs, 'readdirSync').callsFake((dirPath: PathLike) => {
58+
const s = String(dirPath);
59+
if (s.includes('content_types')) {
60+
return (FsUtility.prototype.readdir as sinon.SinonStub)(s);
61+
}
62+
return originalReaddirSync(dirPath);
63+
});
64+
sinon.stub(fs, 'readFileSync').callsFake((filePath: string | Buffer | URL, encoding?: Parameters<typeof fs.readFileSync>[1]) => {
65+
const p = String(filePath);
66+
if (p.includes('content_types')) {
67+
const r = (FsUtility.prototype.readFile as sinon.SinonStub)(p);
68+
if (r !== undefined) {
69+
return typeof r === 'string' ? r : JSON.stringify(r);
70+
}
71+
}
72+
return originalReadFileSync(filePath, encoding as any);
73+
});
74+
3975
fsUtilityWriteFileStub = sinon.stub(fsUtil, 'writeFile').callsFake(() => {
4076
return Promise.resolve();
4177
});

0 commit comments

Comments
 (0)