Skip to content

Commit fee2b4f

Browse files
committed
feat(core)!: Now writing a new contract for the first time requires changedContracts=OVERWRITE
1 parent 25f7f95 commit fee2b4f

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

packages/case-core/src/__tests__/jest/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { AnyMockDescriptorType } from '@contract-case/case-entities-internal';
2-
import { CaseContractDescription } from '@contract-case/case-plugin-base/case-core/src/entities/contract/types';
32
import {
43
ContractDefinerConnector,
54
ContractVerifierConnector,
65
} from '../../connectors';
76
import type { MultiTestInvoker } from '../../core/executeExample/types';
87
import type { CaseConfig } from '../../core/types';
8+
import { CaseContractDescription } from '../../entities/types';
99

1010
export type CaseJestConfig<T extends AnyMockDescriptorType> =
1111
CaseContractDescription & {

packages/case-core/src/connectors/contractStore/contractFileWriter/contractFileWriter.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,17 @@ const createDirectory = (
4747
}
4848
};
4949

50+
const explicitFilename = (context: HasContractFileConfig) =>
51+
context['_case:currentRun:context:contractFilename'];
52+
5053
const actuallyWriteContract = (
5154
pathToFile: string,
5255
contract: ContractData,
5356
context: HasContractFileConfig,
5457
) => {
5558
if (fs.existsSync(pathToFile)) {
5659
if (
57-
context['_case:currentRun:context:contractFilename'] &&
60+
explicitFilename(context) &&
5861
!context['_case:currentRun:context:overwriteFile']
5962
) {
6063
context.logger.error(
@@ -94,6 +97,24 @@ const actuallyWriteContract = (
9497
}
9598
}
9699

100+
if (context['_case:currentRun:context:changedContracts'] === 'FAIL') {
101+
throw new CaseConfigurationError(
102+
`
103+
Tried to write a new contract to
104+
${pathToFile}
105+
But it didn't exist. This isn't allowed with changedContracts set to fail.
106+
107+
While ContractCase is running in snapshot test mode, it's a failure to
108+
create new contracts. You'll need to re-run your tests with one of:
109+
110+
* The configuration property changedContracts is set to 'OVERWRITE'
111+
* The environment variable CASE_changedContracts=OVERWRITE
112+
`,
113+
'DONT_ADD_LOCATION',
114+
'OVERWRITE_CONTRACTS_NEEDED',
115+
);
116+
}
117+
97118
createDirectory(pathToFile, context);
98119
context.logger.maintainerDebug(`Writing contract to '${pathToFile}'`);
99120
fs.writeFileSync(pathToFile, JSON.stringify(contract, undefined, 2));

packages/case-core/src/index.internals.duplicate-vars.spec.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as fs from 'node:fs';
21
// These imports are our code under test
32
import {
43
HttpRequestConsumerSetup,
@@ -22,21 +21,13 @@ const contractDetails = {
2221
const FILENAME = `case-contracts/internal-variable-tests.case.json`;
2322

2423
describe('e2e http consumer driven', () => {
25-
beforeAll(() => {
26-
// Delete the contract file first
27-
try {
28-
fs.rmSync(FILENAME);
29-
fs.mkdirSync('case-contracts');
30-
} catch (e) {
31-
// We don't care if this fails
32-
}
33-
});
3424
defineContract(
3525
{
3626
...contractDetails,
3727
config: {
3828
printResults: false, // Set this to true for you own tests
3929
contractFilename: FILENAME, // Usually you will not need to provide a filename
30+
changedContracts: 'OVERWRITE',
4031
publish: false,
4132
},
4233
},

0 commit comments

Comments
 (0)