Skip to content

Commit

Permalink
Merge pull request #1339 from sasjs/issue-1206
Browse files Browse the repository at this point in the history
fix(compile): terminate compilation if testSetUp or testTearDown failed
  • Loading branch information
YuryShkoda authored Mar 30, 2023
2 parents 475113d + e331d4d commit aee63ef
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 11 deletions.
18 changes: 8 additions & 10 deletions src/commands/compile/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
listSubFoldersInFolder
} from '@sasjs/utils'
import { SASJsFileType, StreamConfig, Target } from '@sasjs/utils/types'
import { prefixMessage } from '@sasjs/utils/error'
import path from 'path'
import {
getMacroFolders,
Expand Down Expand Up @@ -157,10 +158,11 @@ export async function compileJobsServicesTests(
true,
false,
compileTree
).catch((err) =>
process.logger?.error('Test set up compilation has failed.')
)
).catch((err) => {
throw prefixMessage(err, 'Test set up compilation has failed. ')
})
}

if (testTearDown) {
await compileTestFile(
target,
Expand All @@ -169,9 +171,9 @@ export async function compileJobsServicesTests(
true,
false,
compileTree
).catch((err) =>
process.logger?.error('Test tear down compilation has failed.')
)
).catch((err) => {
throw prefixMessage(err, 'Test tear down compilation has failed. ')
})
}

await asyncForEach(serviceFolders, async (serviceFolder) => {
Expand All @@ -194,10 +196,6 @@ export async function compileJobsServicesTests(
)
})
} catch (error) {
process.logger?.error(
'An error has occurred when compiling your jobs and services.'
)

throw error
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/commands/compile/compileCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ export class CompileCommand extends TargetCommand {
return ReturnCode.Success
})
.catch((err) => {
displayError(err, 'An error has occurred when compiling services.')
displayError(err, 'An error has occurred during compilation.')

return ReturnCode.InternalError
})
}
Expand Down
45 changes: 45 additions & 0 deletions src/commands/compile/spec/compile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import { compileSingleFile } from '../compileSingleFile'
import * as compileFile from '../internal/compileFile'
import { setConstants } from '../../../utils'
import { CompileCommand } from '../compileCommand'
import { prefixMessage } from '@sasjs/utils/error'
import * as compileTestFileModule from '../internal/compileTestFile'

describe('sasjs compile', () => {
let sharedAppName: string
Expand All @@ -40,6 +42,7 @@ describe('sasjs compile', () => {
sharedAppName = `cli-tests-compile-${generateTimestamp()}`
await createTestApp(homedir, sharedAppName)
})

beforeEach(async () => {
appName = `cli-tests-compile-${generateTimestamp()}`
await createTestApp(__dirname, appName)
Expand Down Expand Up @@ -163,6 +166,48 @@ describe('sasjs compile', () => {
expect(compileModule.copyFilesToBuildFolder).not.toHaveBeenCalled()
expect(compileModule.compileJobsServicesTests).not.toHaveBeenCalled()
})

it('should fail to compile if testSetUp is present and test file compilation failed', async () => {
const testSetUpFile = 'WRONG.sas'
const compileTestFileError = `ENOENT: no such file or directory, open '${testSetUpFile}'`

const testTarget: any = { ...target }
testTarget.testConfig = { testSetUp: testSetUpFile }

jest
.spyOn(compileTestFileModule, 'compileTestFile')
.mockImplementation(() => Promise.reject(compileTestFileError))

const expectedError = prefixMessage(
compileTestFileError,
'Test set up compilation has failed. '
)

await expect(compileModule.compile(testTarget)).rejects.toEqual(
expectedError
)
})

it('should fail to compile if testTearDown is present and test file compilation failed', async () => {
const testTearDownFile = 'WRONG.sas'
const compileTestFileError = `ENOENT: no such file or directory, open '${testTearDownFile}'`

const testTarget: any = { ...target }
testTarget.testConfig = { testTearDown: testTearDownFile }

jest
.spyOn(compileTestFileModule, 'compileTestFile')
.mockImplementation(() => Promise.reject(compileTestFileError))

const expectedError = prefixMessage(
compileTestFileError,
'Test tear down compilation has failed. '
)

await expect(compileModule.compile(testTarget)).rejects.toEqual(
expectedError
)
})
})

describe('sasjs compile single file', () => {
Expand Down

0 comments on commit aee63ef

Please sign in to comment.