1+ import assert from 'node:assert/strict' ;
12import { normalize } from 'node:path' ;
23import { Readable } from 'node:stream' ;
3-
4- import generateBlogData from '@/next-data/generators/blogData.mjs' ;
4+ import { describe , it , mock } from 'node:test' ;
55
66let files = [ ] ;
77
8- jest . mock ( 'node:fs' , ( ) => ( {
9- createReadStream : jest . fn ( filename => {
10- const readable = new Readable ( ) ;
11- const file = files . find ( f => filename . endsWith ( normalize ( f . path ) ) ) ;
12- readable . push ( `---\n` ) ;
13- file . frontMatterContent . forEach ( line => readable . push ( `${ line } \n` ) ) ;
14- readable . push ( `---\n` ) ;
15- readable . push ( null ) ;
16- readable . close = ( ) => { } ;
17- return readable ;
18- } ) ,
19- } ) ) ;
20-
21- jest . mock ( '../../../next.helpers.mjs' , ( ) => ( {
22- getMarkdownFiles : ( ) => Promise . resolve ( files . map ( file => file . path ) ) ,
23- } ) ) ;
8+ mock . module ( 'node:fs' , {
9+ namedExports : {
10+ createReadStream : filename => {
11+ const readable = new Readable ( ) ;
12+ const file = files . find ( f => filename . endsWith ( normalize ( f . path ) ) ) ;
13+ readable . push ( `---\n` ) ;
14+ file . frontMatterContent . forEach ( line => readable . push ( `${ line } \n` ) ) ;
15+ readable . push ( `---\n` ) ;
16+ readable . push ( null ) ;
17+ readable . close = ( ) => { } ;
18+ return readable ;
19+ } ,
20+ } ,
21+ } ) ;
22+
23+ mock . module ( '../../../next.helpers.mjs' , {
24+ namedExports : {
25+ getMarkdownFiles : ( ) => {
26+ return Promise . resolve ( files . map ( file => file . path ) ) ;
27+ } ,
28+ } ,
29+ } ) ;
30+
31+ const generateBlogData = ( await import ( '../blogData.mjs' ) ) . default ;
2432
2533describe ( 'generateBlogData' , ( ) => {
26- it ( 'should return zero posts and only the default "all" category is no md file is found' , async ( ) => {
34+ it ( 'should return zero posts and only the default "all" category if no md file is found' , async ( ) => {
2735 files = [ ] ;
2836
2937 const blogData = await generateBlogData ( ) ;
3038
31- expect ( blogData . categories ) . toStrictEqual ( [ 'all' ] ) ;
32- expect ( blogData . posts ) . toStrictEqual ( [ ] ) ;
39+ assert . deepEqual ( blogData . categories , [ 'all' ] ) ;
40+ assert . deepEqual ( blogData . posts , [ ] ) ;
41+ } ) ;
42+
43+ it ( 'should collect the data from a single md file if only one is found' , async ( ) => {
44+ files = [
45+ {
46+ path : 'pages/en/blog/post1.md' ,
47+ frontMatterContent : [
48+ `date: '2020-01-01T00:00:00.000Z'` ,
49+ `title: POST 1` ,
50+ `author: author` ,
51+ ] ,
52+ } ,
53+ ] ;
54+
55+ const blogData = await generateBlogData ( ) ;
56+
57+ assert . equal ( blogData . posts . length , 1 ) ;
58+ const post = blogData . posts [ 0 ] ;
59+ assert . equal ( post . title , 'POST 1' ) ;
60+ assert . deepEqual ( post . date , new Date ( '2020-01-01T00:00:00.000Z' ) ) ;
61+ assert . equal ( post . author , 'author' ) ;
3362 } ) ;
3463
3564 it ( 'should collect the data from a single md file if only one is found' , async ( ) => {
@@ -46,11 +75,11 @@ describe('generateBlogData', () => {
4675
4776 const blogData = await generateBlogData ( ) ;
4877
49- expect ( blogData . posts . length ) . toBe ( 1 ) ;
78+ assert . equal ( blogData . posts . length , 1 ) ;
5079 const post = blogData . posts [ 0 ] ;
51- expect ( post . title ) . toEqual ( 'POST 1' ) ;
52- expect ( post . date ) . toEqual ( new Date ( '2020-01-01T00:00:00.000Z' ) ) ;
53- expect ( post . author ) . toEqual ( 'author' ) ;
80+ assert . equal ( post . title , 'POST 1' ) ;
81+ assert . deepEqual ( post . date , new Date ( '2020-01-01T00:00:00.000Z' ) ) ;
82+ assert . equal ( post . author , 'author' ) ;
5483 } ) ;
5584
5685 it ( 'should collect the data from multiple md files' , async ( ) => {
@@ -85,22 +114,25 @@ describe('generateBlogData', () => {
85114
86115 const blogData = await generateBlogData ( ) ;
87116
88- expect ( blogData . posts . length ) . toBe ( 3 ) ;
89- expect ( blogData . posts [ 0 ] . title ) . toEqual ( 'POST 1' ) ;
90- expect ( blogData . posts [ 0 ] . date ) . toEqual (
117+ assert ( blogData . posts . length , 3 ) ;
118+ assert . equal ( blogData . posts [ 0 ] . title , 'POST 1' ) ;
119+ assert . deepEqual (
120+ blogData . posts [ 0 ] . date ,
91121 new Date ( '2020-01-01T00:00:00.000Z' )
92122 ) ;
93- expect ( blogData . posts [ 0 ] . author ) . toEqual ( 'author-a' ) ;
94- expect ( blogData . posts [ 1 ] . title ) . toEqual ( 'POST 2' ) ;
95- expect ( blogData . posts [ 1 ] . date ) . toEqual (
123+ assert . equal ( blogData . posts [ 0 ] . author , 'author-a' ) ;
124+ assert . equal ( blogData . posts [ 1 ] . title , 'POST 2' ) ;
125+ assert . deepEqual (
126+ blogData . posts [ 1 ] . date ,
96127 new Date ( '2020-01-02T00:00:00.000Z' )
97128 ) ;
98- expect ( blogData . posts [ 1 ] . author ) . toEqual ( 'author-b' ) ;
99- expect ( blogData . posts [ 2 ] . title ) . toEqual ( 'POST 3' ) ;
100- expect ( blogData . posts [ 2 ] . date . setMilliseconds ( 0 ) ) . toEqual (
129+ assert . equal ( blogData . posts [ 1 ] . author , 'author-b' ) ;
130+ assert . equal ( blogData . posts [ 2 ] . title , 'POST 3' ) ;
131+ assert . equal (
132+ blogData . posts [ 2 ] . date . setMilliseconds ( 0 ) ,
101133 currentDate . setMilliseconds ( 0 )
102134 ) ;
103- expect ( blogData . posts [ 2 ] . author ) . toEqual ( 'author-c' ) ;
135+ assert . equal ( blogData . posts [ 2 ] . author , 'author-c' ) ;
104136 } ) ;
105137
106138 it ( 'should generate categories based on the categories of md files and their years' , async ( ) => {
@@ -137,7 +169,7 @@ describe('generateBlogData', () => {
137169
138170 const blogData = await generateBlogData ( ) ;
139171
140- expect ( blogData . categories . sort ( ) ) . toStrictEqual ( [
172+ assert . deepEqual ( blogData . categories . sort ( ) , [
141173 'all' ,
142174 'category-a' ,
143175 'category-b' ,
@@ -168,7 +200,7 @@ describe('generateBlogData', () => {
168200
169201 const blogData = await generateBlogData ( ) ;
170202
171- expect ( blogData . posts . map ( p => p . slug ) . sort ( ) ) . toStrictEqual ( [
203+ assert . deepEqual ( blogData . posts . map ( p => p . slug ) . sort ( ) , [
172204 '/blog/category-a/post1' ,
173205 '/blog/category-b/post2' ,
174206 '/blog/uncategorized/post3' ,
0 commit comments