Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest - loading text files for string assertions

Tags:

jestjs

I am working on a text generator and I would like to compare the generated string with a text stored in a sample files. Files have indentation for some lines and it is very cumbersome to construct these strings in TS/js

Is there a simple way to load text from folder relative to current test or even project root in Jest?

like image 209
Jar Avatar asked Mar 14 '26 13:03

Jar


1 Answers

Try this to import your txt into the jest file then compare with it:

const fs = require("fs");
const path = require("path");

const file = path.join(__dirname, "./", "bla.txt");
const fdr = fs.readFileSync(file, "utf8", function(err: any, data: any) {
  return data;
});

expect(string).toBe(fdr)
like image 181
Avshalom N. Israel Avatar answered Mar 17 '26 01:03

Avshalom N. Israel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!