1
1
const path = require ( `path` )
2
2
3
3
const { createFileNode } = require ( `../create-file-node` )
4
+ const fs = require ( `fs-extra` )
5
+
6
+ const fsStatBak = fs . stat
4
7
5
8
// FIXME: This test needs to not use snapshots because of file differences
6
9
// and locations across users and CI systems
7
10
describe ( `create-file-node` , ( ) => {
8
- it ( `creates a file node` , ( ) => {
11
+ beforeEach ( ( ) => {
12
+ // If this breaks, note that the actual values here are not relevant. They just need to be mocked because
13
+ // otherwise the tests change due to changing timestamps. The returned object should mimic the real fs.stat
14
+ // Note: async tests should run in serial so this mock should not cause cross test polution on this thread.
15
+ fs . stat = jest . fn ( ) . mockResolvedValue (
16
+ Promise . resolve ( {
17
+ isDirectory ( ) {
18
+ return false
19
+ } ,
20
+ dev : 123456 ,
21
+ mode : 123456 ,
22
+ nlink : 123456 ,
23
+ uid : 123456 ,
24
+ rdev : 123456 ,
25
+ blksize : 123456 ,
26
+ ino : 123456 ,
27
+ size : 123456 ,
28
+ blocks : 123456 ,
29
+ atimeMs : 123456 ,
30
+ mtimeMs : 123456 ,
31
+ ctimeMs : 123456 ,
32
+ birthtimeMs : 123456 ,
33
+ atime : new Date ( 123456 ) ,
34
+ mtime : new Date ( 123456 ) ,
35
+ ctime : new Date ( 123456 ) ,
36
+ birthtime : new Date ( 123456 ) ,
37
+ } )
38
+ )
39
+ } )
40
+
41
+ afterEach ( ( ) => {
42
+ fs . stat = fsStatBak
43
+ } )
44
+
45
+ it ( `creates a file node` , async ( ) => {
9
46
const createNodeId = jest . fn ( )
10
47
createNodeId . mockReturnValue ( `uuid-from-gatsby` )
11
48
return createFileNode (
@@ -14,4 +51,90 @@ describe(`create-file-node`, () => {
14
51
{ }
15
52
)
16
53
} )
54
+
55
+ it ( `records the shape of the node` , async ( ) => {
56
+ const dname = fs . mkdtempSync ( `gatsby-create-file-node-test` ) . trim ( )
57
+ try {
58
+ const fname = path . join ( dname , `f` )
59
+ console . log ( dname , fname )
60
+ fs . writeFileSync ( fname , `data` )
61
+ try {
62
+ const createNodeId = jest . fn ( )
63
+ createNodeId . mockReturnValue ( `uuid-from-gatsby` )
64
+
65
+ const node = await createFileNode ( fname , createNodeId , { } )
66
+
67
+ // Sanitize all filenames
68
+ Object . keys ( node ) . forEach ( key => {
69
+ if ( typeof node [ key ] === `string` ) {
70
+ node [ key ] = node [ key ] . replace ( new RegExp ( dname , `g` ) , `<DIR>` )
71
+ node [ key ] = node [ key ] . replace ( new RegExp ( fname , `g` ) , `<FILE>` )
72
+ }
73
+ } )
74
+ Object . keys ( node . internal ) . forEach ( key => {
75
+ if ( typeof node . internal [ key ] === `string` ) {
76
+ node . internal [ key ] = node . internal [ key ] . replace (
77
+ new RegExp ( dname , `g` ) ,
78
+ `<DIR>`
79
+ )
80
+ node . internal [ key ] = node . internal [ key ] . replace (
81
+ new RegExp ( fname , `g` ) ,
82
+ `<FILE>`
83
+ )
84
+ }
85
+ } )
86
+
87
+ // Note: this snapshot should update if the mock above is changed
88
+ expect ( node ) . toMatchInlineSnapshot ( `
89
+ Object {
90
+ "absolutePath": "<DIR>/f",
91
+ "accessTime": "1970-01-01T00:02:03.456Z",
92
+ "atime": "1970-01-01T00:02:03.456Z",
93
+ "atimeMs": 123456,
94
+ "base": "f",
95
+ "birthTime": "1970-01-01T00:02:03.456Z",
96
+ "birthtime": "1970-01-01T00:02:03.456Z",
97
+ "birthtimeMs": 123456,
98
+ "blksize": 123456,
99
+ "blocks": 123456,
100
+ "changeTime": "1970-01-01T00:02:03.456Z",
101
+ "children": Array [],
102
+ "ctime": "1970-01-01T00:02:03.456Z",
103
+ "ctimeMs": 123456,
104
+ "dev": 123456,
105
+ "dir": "<DIR>",
106
+ "ext": "",
107
+ "extension": "",
108
+ "id": "uuid-from-gatsby",
109
+ "ino": 123456,
110
+ "internal": Object {
111
+ "contentDigest": "8d777f385d3dfec8815d20f7496026dc",
112
+ "description": "File \\"<DIR>/f\\"",
113
+ "mediaType": "application/octet-stream",
114
+ "type": "File",
115
+ },
116
+ "mode": 123456,
117
+ "modifiedTime": "1970-01-01T00:02:03.456Z",
118
+ "mtime": "1970-01-01T00:02:03.456Z",
119
+ "mtimeMs": 123456,
120
+ "name": "f",
121
+ "nlink": 123456,
122
+ "parent": null,
123
+ "prettySize": "123 kB",
124
+ "rdev": 123456,
125
+ "relativeDirectory": "<DIR>",
126
+ "relativePath": "<DIR>/f",
127
+ "root": "",
128
+ "size": 123456,
129
+ "sourceInstanceName": "__PROGRAMMATIC__",
130
+ "uid": 123456,
131
+ }
132
+ ` )
133
+ } finally {
134
+ fs . unlinkSync ( fname )
135
+ }
136
+ } finally {
137
+ fs . rmdirSync ( dname )
138
+ }
139
+ } )
17
140
} )
0 commit comments