(schema: string, error: string | RegExp)
| 173 | } |
| 174 | |
| 175 | export async function loadSchemaWithError(schema: string, error: string | RegExp) { |
| 176 | if (!schema.includes('datasource ')) { |
| 177 | schema = `${makePrelude('sqlite')}\n\n${schema}`; |
| 178 | } |
| 179 | |
| 180 | // create a temp file |
| 181 | const tempFile = path.join(os.tmpdir(), `zenstack-schema-${crypto.randomUUID()}.zmodel`); |
| 182 | fs.writeFileSync(tempFile, schema); |
| 183 | const r = await loadDocumentWithPlugins(tempFile); |
| 184 | expect(r.success).toBe(false); |
| 185 | invariant(!r.success); |
| 186 | if (typeof error === 'string') { |
| 187 | expect(r).toSatisfy( |
| 188 | (r) => r.errors.some((e: any) => e.toString().toLowerCase().includes(error.toLowerCase())), |
| 189 | `Expected error message to include "${error}" but got: ${r.errors.map((e: any) => e.toString()).join(', ')}`, |
| 190 | ); |
| 191 | } else { |
| 192 | expect(r).toSatisfy( |
| 193 | (r) => r.errors.some((e: any) => error.test(e)), |
| 194 | `Expected error message to match "${error}" but got: ${r.errors.map((e: any) => e.toString()).join(', ')}`, |
| 195 | ); |
| 196 | } |
| 197 | } |
no test coverage detected