Cube outline is see-through

I was working on outlining 3D cubes in OpenGL and I wanted depth testing to be enabled when drawing the outlines, so that other cubes wouldn’t render on top of the outline. When I look to the right using the virtual camera, the stencils are drawn perfectly with the depth testing enabled.

However, when I look to the left, the depth testing for the outlines doesn’t really work and you can kind of see through the outlines. I’m using stencil testing for the outlines. The outlines are black.

This is how it looks when I look to the right:

And this is how it looks when I look to the left:

EDIT: The depth testing for the outlines doesn’t work if I look down (X:0, Y:-1, Z:0) or backwards (X:0, Y:0, Z:1).