Skip to content

Commit

Permalink
Merge pull request #1 from derberg/compile
Browse files Browse the repository at this point in the history
test: update tests
  • Loading branch information
Gmin2 authored Aug 21, 2024
2 parents da7cc75 + 2f6e74f commit 04522a5
Show file tree
Hide file tree
Showing 4 changed files with 3,110 additions and 1,759 deletions.
1 change: 1 addition & 0 deletions apps/generator/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ output
out/
coverage
test/temp/integrationTestResult
test/temp/reactTemplate
test/test-project/package-lock.json
test/test-project/verdaccio/storage/
test/test-project/storage/
3 changes: 2 additions & 1 deletion apps/generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"jsdoc-to-markdown": "^7.1.1",
"markdown-toc": "^1.2.0",
"rimraf": "^3.0.2",
"unixify": "^1.0.0"
"unixify": "^1.0.0",
"fs-extra": "9.1.0"
}
}
34 changes: 21 additions & 13 deletions apps/generator/test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
*/

const path = require('path');
const { readFile, writeFile, access, unlink, mkdir } = require('fs').promises;
const { readFile, writeFile, access, mkdir } = require('fs').promises;
const { copy } = require('fs-extra');
const Generator = require('../lib/generator');
const dummySpecPath = path.resolve(__dirname, './docs/dummy.yml');
const refSpecPath = path.resolve(__dirname, './docs/apiwithref.json');
Expand All @@ -12,13 +13,22 @@ const crypto = require('crypto');
const mainTestResultPath = 'test/temp/integrationTestResult';
const reactTemplate = 'test/test-templates/react-template';
const nunjucksTemplate = 'test/test-templates/nunjucks-template';
//temp location where react template is copied for each test that does some mutation on template files
const copyOfReactTemplate = 'test/temp/reactTemplate';

describe('Integration testing generateFromFile() to make sure the result of the generation is not changend comparing to snapshot', () => {
const generateFolderName = () => {
//you always want to generate to new directory to make sure test runs in clear environment
return path.resolve(mainTestResultPath, crypto.randomBytes(4).toString('hex'));
};

const getCleanReactTemplate = async () => {
//for each test new react template is needed in unique location
const newReactTemplateLocation = path.resolve(copyOfReactTemplate, crypto.randomBytes(4).toString('hex'));
await copy(reactTemplate, newReactTemplateLocation);
return newReactTemplateLocation;
};

jest.setTimeout(100000);
const testOutputFile = 'test-file.md';

Expand Down Expand Up @@ -70,12 +80,14 @@ describe('Integration testing generateFromFile() to make sure the result of the

it('check if the temp.md file is created with compile option true', async () => {
const outputDir = generateFolderName();

const cleanReactTemplate = await getCleanReactTemplate();
// Create temp.md.js file dynamically

const tempJsPath = path.join(cleanReactTemplate, 'template/temp.md.js');
// Create temp.md.js file dynamically
const tempJsPath = path.join(reactTemplate, 'template/temp.md.js');
await writeFile(tempJsPath, tempJsContent);

const generator = new Generator(reactTemplate, outputDir, {
const generator = new Generator(cleanReactTemplate, outputDir, {
forceWrite: true,
compile: true,
debug: true,
Expand All @@ -91,17 +103,12 @@ describe('Integration testing generateFromFile() to make sure the result of the

it('check if the temp.md file is not created when compile option is false', async () => {
const outputDir = generateFolderName();

// first we need to do cleanup of the react template `__transpiled` folder as from previous test it will have the transpiled files
const transpiledPath = path.join(reactTemplate, '__transpiled');
await unlink(path.join(transpiledPath, 'temp.md.js'));
await unlink(path.join(transpiledPath, 'temp.md.js.map'));

const cleanReactTemplate = await getCleanReactTemplate();
// Create temp.md.js file dynamically
const tempJsPath = path.join(reactTemplate, 'template/temp.md.js');
const tempJsPath = path.join(cleanReactTemplate, 'template/temp.md.js');
await writeFile(tempJsPath, tempJsContent);

const generator = new Generator(reactTemplate, outputDir, {
const generator = new Generator(cleanReactTemplate, outputDir, {
forceWrite: true,
compile: false,
debug: true
Expand All @@ -116,6 +123,7 @@ describe('Integration testing generateFromFile() to make sure the result of the

it('should ignore specified files with noOverwriteGlobs', async () => {
const outputDir = generateFolderName();
const cleanReactTemplate = await getCleanReactTemplate();
// Manually create a file to test if it's not overwritten
await mkdir(outputDir, { recursive: true });
// Create a variable to store the file content
Expand All @@ -125,7 +133,7 @@ describe('Integration testing generateFromFile() to make sure the result of the
await writeFile(testFilePath, testContent);

// Manually create an output first, before generation, with additional custom file to validate if later it is still there, not overwritten
const generator = new Generator(reactTemplate, outputDir, {
const generator = new Generator(cleanReactTemplate, outputDir, {
forceWrite: true,
noOverwriteGlobs: [`**/${testOutputFile}`],
debug: true,
Expand Down
Loading

0 comments on commit 04522a5

Please sign in to comment.