|
| 1 | +/* eslint-disable no-underscore-dangle */ |
| 2 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 3 | + |
| 4 | +import { type Task, modifyErrorMessage } from './setup-file'; |
| 5 | + |
| 6 | +describe('modifyErrorMessage', () => { |
| 7 | + const originalUrl = import.meta.env.__STORYBOOK_URL__; |
| 8 | + beforeEach(() => { |
| 9 | + import.meta.env.__STORYBOOK_URL__ = 'http://localhost:6006'; |
| 10 | + }); |
| 11 | + |
| 12 | + afterEach(() => { |
| 13 | + import.meta.env.__STORYBOOK_URL__ = originalUrl; |
| 14 | + }); |
| 15 | + |
| 16 | + it('should modify the error message if the test is failing and there is a storyId in the task meta', () => { |
| 17 | + const task: Task = { |
| 18 | + type: 'test', |
| 19 | + result: { |
| 20 | + state: 'fail', |
| 21 | + errors: [{ message: 'Original error message' }], |
| 22 | + }, |
| 23 | + meta: { storyId: 'my-story' }, |
| 24 | + }; |
| 25 | + |
| 26 | + modifyErrorMessage({ task }); |
| 27 | + |
| 28 | + expect(task.result?.errors?.[0].message).toMatchInlineSnapshot(` |
| 29 | + " |
| 30 | + [34mClick to debug the error directly in Storybook: http://localhost:6006/?path=/story/my-story&addonPanel=storybook/interactions/panel[39m |
| 31 | +
|
| 32 | + Original error message" |
| 33 | + `); |
| 34 | + expect(task.result?.errors?.[0].message).toContain('Original error message'); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should not modify the error message if task type is not "test"', () => { |
| 38 | + const task: Task = { |
| 39 | + type: 'custom', |
| 40 | + result: { |
| 41 | + state: 'fail', |
| 42 | + errors: [{ message: 'Original error message' }], |
| 43 | + }, |
| 44 | + meta: { storyId: 'my-story' }, |
| 45 | + }; |
| 46 | + |
| 47 | + modifyErrorMessage({ task }); |
| 48 | + |
| 49 | + expect(task.result?.errors?.[0].message).toBe('Original error message'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should not modify the error message if task result state is not "fail"', () => { |
| 53 | + const task: Task = { |
| 54 | + type: 'test', |
| 55 | + result: { |
| 56 | + state: 'pass', |
| 57 | + }, |
| 58 | + meta: { storyId: 'my-story' }, |
| 59 | + }; |
| 60 | + |
| 61 | + modifyErrorMessage({ task }); |
| 62 | + |
| 63 | + expect(task.result?.errors).toBeUndefined(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should not modify the error message if meta.storyId is not present', () => { |
| 67 | + const task: Task = { |
| 68 | + type: 'test', |
| 69 | + result: { |
| 70 | + state: 'fail', |
| 71 | + errors: [{ message: 'Non story test failure' }], |
| 72 | + }, |
| 73 | + meta: {}, |
| 74 | + }; |
| 75 | + |
| 76 | + modifyErrorMessage({ task }); |
| 77 | + |
| 78 | + expect(task.result?.errors?.[0].message).toBe('Non story test failure'); |
| 79 | + }); |
| 80 | +}); |
0 commit comments