Implement toMatchSnapshot() (#2294)

* buggy snapshot

* error output for failed snapshot

* missing first

* hints

* open dir once, better cleanup

* update flag

* truncate on update

* object and class snapshot formatting

* array formatting

* no function name, single item is empty array

* string objects, maps, sets, promise

* avoid using invalid memory

* handle number objects

* handle extending `Number`

* boolean objects

* snapshot tests and test updates

* snapshot format for buffers

* safer snapshot parsing

* property matchers setup

* strings and tests

* generate classes with empty prototype

* optional `propertyMatchers` parameter

* new test folder structure

* strings.eqlLong

* globalObject.throwPretty() and expect.any tests

* add updateSnapshot flag to help

* move snapshot format out of `printErrorlikeObject`

* empty object snapshot format

* separate typed array, remove trailing comma

* use `isCell`, object trailing commas

* handle unicode

* todo for primitive constructors

* switch to `JSC.Node.Syscall.open` and `JSC.Maybe`

* use js parser for snapshot files

* deinit ast, log parse error

* copy/paste most of `exports.ZigConsoleClient`

* remove snapshot option

* remove ordered properties option

* remove snapshot format option from `exports.zig`

* remove extra newlines

* change mode

* update test runner output

* escape backticks faster

* `bunx jest` in temp dir

* remove buffered writer

* add `toMatchSnapshot` to types

* cleanup, switch to `pread`

* cli `--update` flag

* `--update-snapshots`

* remove string object format
This commit is contained in:
Dylan Conway
2023-03-14 16:50:59 -07:00
committed by GitHub
parent 76b875e414
commit 4792abdb7f
41 changed files with 4635 additions and 131 deletions

View File

@@ -41,6 +41,7 @@ const HTTPThread = @import("bun").HTTP.HTTPThread;
const JSC = @import("bun").JSC;
const jest = JSC.Jest;
const TestRunner = JSC.Jest.TestRunner;
const Snapshots = JSC.Jest.Snapshots;
const Test = TestRunner.Test;
const NetworkThread = @import("bun").HTTP.NetworkThread;
const uws = @import("bun").uws;
@@ -365,12 +366,23 @@ pub const TestCommand = struct {
bun.JSC.initialize();
HTTPThread.init() catch {};
var snapshot_file_buf = std.ArrayList(u8).init(ctx.allocator);
var snapshot_values = Snapshots.ValuesHashMap.init(ctx.allocator);
var snapshot_counts = bun.StringHashMap(usize).init(ctx.allocator);
var reporter = try ctx.allocator.create(CommandLineReporter);
reporter.* = CommandLineReporter{
.jest = TestRunner{
.allocator = ctx.allocator,
.log = ctx.log,
.callback = undefined,
.snapshots = Snapshots{
.allocator = ctx.allocator,
.update_snapshots = ctx.test_options.update_snapshots,
.file_buf = &snapshot_file_buf,
.values = &snapshot_values,
.counts = &snapshot_counts,
},
},
.callback = undefined,
};
@@ -421,6 +433,8 @@ pub const TestCommand = struct {
runAllTests(reporter, vm, test_files, ctx.allocator);
}
try jest.Jest.runner.?.snapshots.writeSnapshotFile();
if (reporter.summary.pass > 20) {
if (reporter.summary.skip > 0) {
Output.prettyError("\n<r><d>{d} tests skipped:<r>\n", .{reporter.summary.skip});
@@ -481,6 +495,40 @@ pub const TestCommand = struct {
Output.prettyError(" {d:5>} fail<r>\n", .{reporter.summary.fail});
if (reporter.jest.snapshots.total > 0) {
const passed = reporter.jest.snapshots.passed;
const failed = reporter.jest.snapshots.failed;
const added = reporter.jest.snapshots.added;
var first = true;
Output.prettyError(" <d>snapshots:<r> ", .{});
if (passed > 0) {
Output.prettyError("<d>{d} passed<r>", .{passed});
first = false;
}
if (added > 0) {
if (first) {
first = false;
Output.prettyError("<d>{d} added<r>", .{added});
} else {
Output.prettyError("<d>, {d} added<r>", .{added});
}
}
if (failed > 0) {
if (first) {
first = false;
Output.prettyError("<red>{d} failed<r>", .{failed});
} else {
Output.prettyError(", <red>{d} failed<r>", .{failed});
}
}
Output.prettyError("\n", .{});
}
if (reporter.summary.expectations > 0) Output.prettyError(" {d:5>} expect() calls\n", .{reporter.summary.expectations});
Output.prettyError("Ran {d} tests across {d} files ", .{